How to compress files with tar command on Linux/Unix

The tar command is commonly used to compress and archive files in Linux/Unix systems. The basic syntax for compressing files with tar is:

tar -czvf archive.tar.gz file1 file2 ...

Here’s a breakdown of the options used in the command:

  • -c: create a new archive
  • -z: compress the archive using gzip compression
  • -v: verbose mode, display progress information
  • -f: use archive file specified after this option as the archive file name

So, in the example above, the tar command creates a new archive named archive.tar.gz that contains the files file1 and file2, and compresses the archive using gzip compression.

You can also use the tar command to compress an entire directory, including all its subdirectories, by simply specifying the directory name instead of individual file names:

tar -czvf archive.tar.gz directory_name

To decompress a tar.gz archive, you can use the following command:

tar -xzvf archive.tar.gz

Note: The -x option is used to extract files from the archive.

Leave a Comment