Compression and Archiving with zip, gzip, bzip2 and tar
Compression tools are used to save one or more files to a single file to make space on the disk. Compression tools are very important in the linux such that we can transfer the compressed files to remote systems and these are easily downloadable via the internet.
Note: Archived file uses the same amount of disk space as all the individual files and directories combined.However, compressed file uses less amount of disk space than all individual files. |
Suggestable Read: Part 2: Archive and Compress Tools
Also Read: Part -1: Linux Archive, Backup, and Compress Tools
There are 3 types of compression tools are available in Linux:
1.gzip(z)
2.bzip2(j)
3.xz(J)
Compression method | Options | File extension | Decompression method |
gzip | z | .gz | gunzip |
bzip2 | b | .bz2 | bzip2 |
xz | J | .xz | ùnxz |
Using compression tools in Linux:
To compress an archive file using different types of compression tools:
1. gzip /tmp/etc.tar
gzip utility will compress the file directly and it will replace with the original file.
[[email protected] tmp]# gzip /tmp/etc.tar |
2. bzip2 /tmp/etc.tar
[[email protected] tmp]# bzip2 /tmp/etc.tar |
3. xz /tmp/etc.tar
[[email protected]echinformant tmp]# xz /tmp/etc.tar |
Decompressing a compressed file: Below commands are used to decompressing the files.
4. gunzip /tmp/etc.tar.gz
This command will decompress the gzip file and also replace the original file if exists.
[[email protected] tmp]# gunzip /tmp/etc.tar.tz |
-rw-r–r–. 1 root root 31549440 Feb 6 09:59 etc.tar
5. bunzip2 /tmp/etc.tar.bz
[[email protected] tmp]# bunzip2 /tmp/etc.tar.tz |
-rw-r–r–. 1 root root 31549440 Feb 6 08:59 etc.tar
6. unxz /tmp/etc.tar.xz
[[email protected] tmp]# unxz /tmp/etc.tar.tz |
-rw-r–r–. 1 root root 31549440 Feb 6 10:59 etc.tar
We can also use tar command to compress the files using above compressed tools:
7. tar -czf /tmp/etc.tar.gz /etc
[[email protected] tmp]# tar -czf /tmp/etc.tar.gz /etc |
-rw-r–r–. 1 root root 8789621 Feb 6 18:59 etc.tar.gz
8. tar -cjf /tmp/etc.tar.bz /etc
[[email protected] tmp]# tar -cjf /tmp/etc.tar.bz /etc |
-rw-r–r–. 1 root root 8789621 Feb 6 23:59 etc.tar.bz2
9. tar -cJf /tmp/etc.tar.xz /etc
[[email protected] tmp]# tar -xJf /tmp/etc.tar.xz /etc |
-rw-r–r–. 1 root root 8789621 Feb 6 27:59 etc.tar.xz