Linux Compression and Archiving Tools

Compression tools are used to compress one or more files. Archiving tools  collect files and their metadata together to produces one file.

We will look at basic usage of following compression and archiving tolls.

gzip and gunzip (can achieve better compression compared to ZIP)

bzip2 and bunzip2

tar (archiving)

zip and unzip (capable of archiving and compressing multiple files)


Canada colocation

Using gzip and gunzip

The gzip command creates a compressed file of each of the files specified at the command line and adds .gz extention to each one of them.

Lets compress files user1.txt, user2.txt, user3.txt, user4.txt

#ls
user1.txt  user2.txt  user3.txt  user4.txt
#gzip user1.txt user2.txt user3.txt user4.txt
#ls
user1.txt.gz  user2.txt.gz  user3.txt.gz  user4.txt.gz

Alternatively you can run gzip * to compress all files in current directory

gzip *

Please note gzip can not compress entire directory. In order to do this we will need to use tar with gzip

tar -zcvf myarchive.tar.gz /directory_to_compress

To uncompress this files we need to run gunzip or gzip -d

#gunzip user1.txt.gz user2.txt.gz 
#ls
user1.txt  user2.txt  user3.txt.gz  user4.txt.gz
#gzip -d user3.txt.gz user4.txt.gz
#ls
user1.txt  user2.txt  user3.txt  user4.txt

Using bzip2 and bunzip2

This is another compression tool similar to gzip. the usage is very similar to gzip also.

#ls
user1.txt  user2.txt  user3.txt  user4.txt
#bzip2 *
#ls
user1.txt.bz2  user2.txt.bz2  user3.txt.bz2  user4.txt.bz2

To uncompress the files use bunzip2 or bzip2 -d

#ls
user1.txt.bz2  user2.txt.bz2  user3.txt.bz2  user4.txt.bz2
#bunzip2 user1.txt.bz2 user2.txt.bz2 
#ls 
user1.txt  user2.txt  user3.txt.bz2  user4.txt.bz2
#bzip2 -d user3.txt.bz2 user4.txt.bz2
#ls
user1.txt  user2.txt  user3.txt  user4.txt

 

Archiving with tar

Tar command creates, appends, updates, lists and extracts files to and from a single file called tarball or tar file. Tar can also be instructed to compress an archive while it is being created. Using -j compresses tarball with bzip2 and -z will use gzip for compression.

man tar

Here is an example of how to tarball test directory and compress it with gzip

#tar cvfz test.tar.gz test/
test/
test/user1.txt
test/user3.txt
test/user2.txt
test/user4.txt
#ls
test  test.tar.gz

Here is an example on tarball with bzip2 compression

#tar cvjf test.tar.bz2 test/
test/
test/1.txt
test/2.txt
test/3.txt
#ls
test test.tar.bz2

Few more useful tar commands
list contents of tar

#tar tvf test.tar.bz2 
2016-03-01 09:54 test/
2016-03-01 09:54 test/1.txt
2016-03-01 09:54 test/2.txt
2016-03-01 09:54 test/3.txt

Restore tar file

#tar xvf test.tar.bz2 
test/
test/1.txt
test/2.txt
test/3.txt
#ls
test  test.tar.bz2

Using zip and unzip

This is another compression and archiving tool that is very popular with windows users but can also be used in Linux
To zip files

zip file.zip file1 file2 file3

Zip directory

#zip -r test.zip test
  adding: test/ (stored 0%)
  adding: test/1.txt (stored 0%)
  adding: test/2.txt (stored 0%)
  adding: test/3.txt (stored 0%)
#ls
test test.zip

To unzip file

unzip file.zip