Cheat Sheet - Archives and Compression

tar

Note: The -f option has to come last, to tell tar what the filename is.

- Create an archive from files:
    tar -cf target.tar file1 file2 file3

- Create a gzipped archive:
    tar -czf target.tar.gz file1 file2 file3

- Create a compressed archive, using archive suffix to determine the compression program:
    tar -caf target.tar.xz file1 file2 file3

- List the contents of a tar file:
    tar -tvf source.tar

- Extract a gzipped archive in the current directory:
    tar -xzf source.tar.gz

- Extract a bzipped archive in the current directory:
    tar -xjf source.tar.bz2

- Extract an archive in a target folder:
    tar -xvf source.tar -C folder
    tar -xvf file.tar etc
    tar -xzvf file.tar.gz etc
    tar -xjvf file.tar.bz2 etc


- Extract a single file (root/subfolder):
    tar -xvf file.tar foo.txt
    tar -xzvf file.tar.gz foo.txt
    tar -xjvf file.tar.bz2 foo.txt

    tar -xvf file.tar etc/resolv.conf
    tar -xzvf file.tar.gz etc/resolv.conf
    tar -xjvf file.tar.bz2 etc/resolv.conf


- Extract files matching a pattern:
    tar -xf source.tar --wildcards "*.html"

unzip

- Extract zip file(s) (for multiple files, separate file paths by spaces):
    unzip file(s)

- Extract zip files(s) to given path:
    unzip compressed_file(s) -d /path/to/put/extracted_file(s)

- List the contents of a zip file without extracting:
    unzip -l file.zip

- Extract the contents of the file(s) to stdout alongside the extracted file names:
    unzip -c file.zip

- Extract a zip file created in windows, containing files with non-ascii (chinese) filenames:
    unzip -O gbk file.zip

zip

- Package and compress a directory and its contents, [r]ecursively:
    zip -r compressed.zip /path/to/dir

- E[x]clude unwanted files from being added to the compressed archive:
    zip -r compressed.zip path/to/dir -x path/to/exclude

- Archive a directory and its contents with the highest level [9] of compression:
    zip -r -9 compressed.zip /path/to/dir

- Package and compress multiple directories and files:
    zip -r compressed.zip /path/to/dir1 /path/to/dir2 /path/to/file

- Create an encrypted archive (user will be prompted for a password):
    zip -e -r compressed.zip path/to/dir

- Add files to an existing zip file:
    zip compressed.zip path/to/file

- Delete files from an existing zip file:
    zip -d compressed.zip "foo/*.tmp"

- Archive a directory and its contents to a multi-part [s]plit zip file (e.g. 3GB parts):
    zip -r -s 3g compressed.zip path/to/dir

gzip

- Compress a file, replacing it with a gzipped compressed version:
    gzip file.ext

- Decompress a file, replacing it with the original uncompressed version:
    gzip -d file.ext.gz

- Compress a file specifying the output filename:
    gzip -c file.ext > compressed_file.ext.gz

- Uncompress a gzipped file specifying the output filename:
    gzip -c -d file.ext.gz > uncompressed_file.ext

- Specify the compression level. 1=Fastest (Worst), 9=Slowest (Best), Default level is 6:
    gzip -9 -c file.ext > compressed_file.ext.gz