Bash Compress and Extract

Learn how to bundle files into a .zip archive and pull them back out again using zip and unzip.

Creating Archives with zip

zip is a general-purpose archive and compression format supported natively on Windows, macOS, and Linux, which makes it the safest choice when you don't know what the recipient will use to open a file. The zip command takes an output archive name followed by the files or folders to include.

Example

$ zip notes.zip report.txt summary.txt
  adding: report.txt (deflated 61%)
  adding: summary.txt (deflated 54%)

Zipping a single file works the same as zipping several, but folders need the -r (recursive) flag so zip walks into subdirectories instead of skipping them. -q suppresses the per-file listing for quieter output, and -9 asks for the maximum compression level at the cost of a little more CPU time.

Example

$ zip -r9 site-backup.zip ./site
  adding: site/ (stored 0%)
  adding: site/index.html (deflated 68%)
  adding: site/assets/logo.png (deflated 2%)
  adding: site/assets/style.css (deflated 71%)

Extracting with unzip

unzip reverses the process. Run alone it extracts into the current directory, but -d lets you send the contents somewhere else without changing directories first. Before extracting into a folder that already has files in it, -l lists the archive's contents without touching disk, so name collisions can be checked first.

Example

$ unzip -l site-backup.zip
Archive:  site-backup.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2026-07-10 09:01   site/
     4821  2026-07-10 09:01   site/index.html
    88213  2026-07-10 09:01   site/assets/logo.png
     2104  2026-07-10 09:01   site/assets/style.css
---------                     -------
    95138                     4 files

$ unzip site-backup.zip -d /var/www/restored
Archive:  site-backup.zip
   creating: /var/www/restored/site/
  inflating: /var/www/restored/site/index.html
  inflating: /var/www/restored/site/assets/logo.png
  inflating: /var/www/restored/site/assets/style.css
  • -r recurses into directories when zipping
  • -q enables quiet mode, suppressing per-file output
  • -9 sets maximum compression level (0-9, default 6)
  • -e encrypts the archive with a password prompt
  • -x excludes matching files from the archive
FlagCommandDescription
-rzipRecurse into directories instead of skipping them
-9zipUse maximum compression, slower but smaller output
-lunzipList archive contents without extracting
-dunzipExtract into the given directory instead of the current one
-nunzipNever overwrite existing files during extraction
Note: Run unzip -l archive.zip before extracting into a directory that already has files, so you know exactly what will land there and whether names collide.
Note: unzip overwrites existing files silently by default. Pass -n to skip files that already exist, or -u to update only files older than the version in the archive.