How to extract or compress a tar.gz file on Linux or Unix using command line
One of the most common commands to create and work with backup archives is the tar command. Archiving and compressing files and directories are very useful when we need to create backups and transfer data across a network. By using this command we can gather large sets of files into a single file (archive).
Tar Command Actions ( one required )
- c (create an archive)
- t (list the contents of an archive)
- x (extract an archive)
- f (file name of the archive to operate on)
- v (verbose)
Example 1 – How to extract/uncompress File
To extract the files use the following command:
tar -xvf sqlbackup.tar
tar -xvf sqlbackup.tar.gz
In the above commands, sqlbackup.tar is the name of the file that we are extracting.
Example 2 – How to compress
To compress the files. Use the following command:
tar -czf /root/etcbackup.tar.gz /etc
There are three different compression methods supported by the tar command. The gzip compression is the fastest. The bzip2 compression usually leads to smaller archive files compared to gzip while the xz compression method is relatively new, but usually offers the best compression ratio of the methods available.
- z for gzip (filename.tar.gz or filename.tgz)
- j for bzip2 (filename.tar.bz2)
- J for xz (filename.tar.xz)
Example 3 – Listing files in archive
tar -tvf sqlbackup.tar.gz
So that was a quick tutorial on the tar command. Have any questions? Leave a comment below.