tar.gz – How to extract or compress a tar.gz file on Linux or Unix

How to extract or compress a tar.gz file on Linux or Unix using command line

tar.gz File

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)

Options

  • 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

Or

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.

Compression Options

  • 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

Summary

So that was a quick tutorial on the tar command. Have any questions? Leave a comment below.

One Response

  1. Abiby May 6, 2017

Add Comment