Linux file compression tools/utilities
In Category Linux
Linux has various types of compression and decompression tools like tar, gzip, bzip2, zip and compress. This article describes practical usage of these utilities.
tar and untar
tar command operates on one or more files or directories and creates an archive. An archive created by tar command is called a tarball. Generally such archives will have .tar as file extension. tar is more of a archiving utility than a compression utility. Typically it is used along with file compression utilities like gzip and bzip2. See various ways of creating a tarball using -cvf option in the following examples.
[liz@techpulp ~]# tar -cvf mydir.tar mydir/ [liz@techpulp ~]# tar -cvf myfiles.tar file1.txt file2.txt file3.c [liz@techpulp ~]# tar -cvf mixedball.tar mydir1/ file1.txt file2.txt file3.c mydir2/
All files in a tarball can be extracted using -xvf option as show below.
[liz@techpulp ~]# tar -xvf mydir.tar [liz@techpulp ~]# tar -xvf myfiles.tar [liz@techpulp ~]# tar -xvf mixedball.tar
gzip and gunzip
This is another set of file compression and decompression utilities. Unlike tar, gzip and gunzip can operate only on a file. The compressed file will have .gz as its file extension.
[liz@techpulp ~]# gzip myfile1.txt
The above command creates myfile1.txt.gz. You can uncompress the file using gunzip command as shown below.
[liz@techpulp ~]# gunzip myfile1.txt.gz
If you want to compress a directory using gzip command, you have to create a tar archive first. Then compress it using gzip command. Typically such an archive will have .tar.gz or .tgz as its file extension. Such archive is also called a tarball.
[liz@techpulp ~]# tar -cvf mydir1.tar mydir1 [liz@techpulp ~]# gzip mydir1.tar
The above example creates mydir1.tar.gz. The tar command provides an option -z to do the both operations at once. The following example is same as above example
[liz@techpulp ~]# tar -zcvf mydir1.tar.gz mydir1
The following command is used to extract files and directories present in a .tar.gz tarball.
[liz@techpulp ~]# tar -zxvf mydir1.tar.gz
bzip2 and bunzip2
This is another set of file compression and decompression utilities similar to gzip and gunzip. But this is slower and does better compression. This also operates only on files. Compression is done as show below.
[liz@techpulp ~]# tar -cvf mydir1.tar mydir1 [liz@techpulp ~]# bzip2 mydir1.tar
The above example creates mydir1.tar.bz2. Descompression is done as shown below.
[liz@techpulp ~]# bunzip2 mydir1.tar.bz2 [liz@techpulp ~]# tar -xvf mydir1.tar
zip and unzip
This tool produces archives with file extension .zip which are compatible with popular Windows operating system. The following example shows how to create a zip archive from files and directories.
[liz@techpulp ~]# zip myfiles.zip file1.txt file2.txt file3.c [liz@techpulp ~]# zip -r mydir1.zip mydir1/
The following example shows how to extract files from a .zip archive
[liz@techpulp ~]# unzip myfiles.zip [liz@techpulp ~]# unzip mydir1.zip
compress and uncompress
These utilities are provided by ncompress package. Typically the archives created using this utility will have .Z as file extension. Similar to gzip and bzip2, this utility operates on files. An example on how to use it along with tar to compress directory is shown below.
[liz@techpulp ~]# tar -cvf mydir1.tar mydir1 [liz@techpulp ~]# compress mydir1.tar
The above example creates mydir1.tar.Z. Descompression is done as shown below.
[liz@techpulp ~]# uncompress mydir1.tar.Z [liz@techpulp ~]# tar -xvf mydir1.tar
Recent Comments