Linux commands to compress, extract & view files and folders

Zip is the most common archiving format. It is avialable on all operative systems such as Linux, Windows and Mac. However, it does not offer the best level of compression as compared to tar.gz and tar.bz2.

Tar is very common archiving format in Linux. Tar.gz is better than tar compression. And, tar.bz2 is the the best among all in the level of compression. It also utilizes the least CPU.

Here are some linux commands to create and extract different kinds of compressions.

1. Zip

Compress a file

zip -r archive_name.zip file_to_compress

Compress a directory

zip -r archive_name.zip folder_to_compress

Extract a zipped file

unzip archive_to_extract.zip

View content of the compressed file

zip -sf your_archive_file.zip

OR

vi your_archive_file.zip

2. Tar

Compress a file

tar -cvf archive_name.tar file_to_compress

Compress a directory

tar -cvf archive_name.tar folder_to_compress

Compress multiple files and/or directories

tar -cvf archive_name.tar file1 file2 folder1 folder2

Extract a tar file

tar -xvf archive_to_extract.tar

View content of the compressed file

tar -tvf your_archive_file.tar

OR

vi your_archive_file.tar

3. Tar.bz2

Compress one file

tar -cvjf archive_name.tar.bz2 file_to_compress

Compress multiple files and/or directories

tar -cvjf archive_name.tar.bz2 file1 file2 folder1 folder2

Extract a tar.bz2 file

tar -xvjf archive_to_extract.tar.bz2

View content of the compressed file

tar -tvf your_archive_file.tar.bz2

OR

vi your_archive_file.tar.bz2

4. Tar.gz

Compress one file

tar -cvzf archive_name.tar.gz file_to_compress

Compress multiple files and/or directories

tar -cvzf archive_name.tar.gz file1 file2 folder1 folder2

Extract a tar.gz file

tar -xvzf archive_to_extract.tar.gz

View content of the compressed file

tar -tvf your_archive_file.tar.gz

OR

vi your_archive_file.tar.gz

5. Bz2

To compress a file (ONLY) to a bz2 file (NOT tar.bz2)

bzip2 -zk file_to_compress

Extract a bz2 file

bunzip2 archive_to_extract.bz2

6. Gzip

To compress a file (ONLY) to a gzip file (NOT tar.gz)

gzip -c file_to_compress > archive_name.gz

Extract a gz file

gzip -d file.gz