Compressing files in Linux & Unix – Cheat Sheet

Here is a quick cheat-sheet on How To Compress files in Linux & Unix

Zip
zip archive file1 file2 file3
Compresses file1 file2 file3 into an achive with filename archive.zip

zip archive *
Compresses all files in the current directory into an achive with filename archive.zip

zip -r archive dir
Compresses the directory dir(and its subdirectories) into an achive with filename archive.zip

unzip archive.zip
Uncompresses the archive archive.zip to the current directory

unzip archive.zip -d dir
Uncompresses the archive archive.zip to the directory dir

Tar

The tar command accepts common parameters. These are:

  • c – create a new archive
  • v – verbosely describe each step of the process
  • f – following is the archive file name
  • t – list the contents of the archive
  • x – extract the archive

tar cvf archive.tar file1 file2 file3
Creates an uncompressed archive of the files file1 file2 file3 with filename archive.tar

tar cvf archive.tar dir
Creates an uncompressed archive of the directory dir with filename archive.tar
tar tvf archive.tar
List all files in the archive archive.tar verbosely

tar -xvf archive.tar
Extract all files in the archive archive.tar

tar xvf archive_file.tar /path/to/file1 /path/to/file2 /path/to/file3
Extracts specific files from the archive archive.tar

tar xvf archive.tar --wildcards '*.txt'
Extracts all files ending with ‘.txt’ from the archive archive.tar

Tar – Bzip

tar cvfj archive.tar.bz2 file1 file2 file3
Creates an archive using the bz2 algorithm of the files file1 file2 file3 with filename archive.tar.bz2

tar cvfj archive.tar.bz2 dir
Creates an archive using the bz2 algorithm of the directory dir with filename archive.tar.bz2

tar -xvfj archive.tar.bz2
Extract all files in the archive archive.tar.bz2

tar xvfj archive_file.tar.bz2 /path/to/file1 /path/to/file2 /path/to/file3
Extracts specific files from the archive archive.tar.bz2

tar xvfj archive.tar.bz2 --wildcards '*.txt'
Extracts all files ending with ‘.txt’ from the archive archive.tar.bz2

Tar – Gzip

tar cvfz archive.tar.gz file1 file2 file3
Creates an archive using the gz algorithm of the files file1 file2 file3 with filename archive.tar.gz

tar cvfz archive.tar.gz dir
Creates an archive using the bz2 algorithm of the directory dir with filename archive.tar.gz

tar -xvfz archive.tar.gz
Extract all files in the archive archive.tar.gz

tar xvf archive_file.tar.gz /path/to/file1 /path/to/file2 /path/to/file3
Extracts a specific files from the archive archive.tar.gz

tar xvf archive.tar.gz --wildcards '*.txt'
Extracts all files ending with ‘.txt’ from the archive archive.tar

Note: Bzip archives takes more time to compress and decompress compared to Gzip.
Bzip archival size is less than Bzip.