• How to create .tar.gz file in Linux from the command line

To create a tar.gz file on Linux open the terminal application in Linux and run tar command to create an archive by running:

tar -czvf filename.tar.gz directory


tar -czvf httpdocs.tar.gz /path/to/dir1
tar -czvf httpdocs.tar.gz /path/to/dir1 dir2 file1 file2

For example, if you want to create tar.gz file named httpdocs.tar.gz for directory called /var/www/vhosts/example.com/httpdocs/ the syntax for the tar command is as follows:

tar -czvf httpdocs.tar.gz /var/www/vhosts/example.com/httpdocs/

Additionally, if you wanted to create a tar.gz file from all .pdf files in a folder:

tar -czvf archive.tgz *.pdf

To create archive.tar.gz in the current working directory, run:

tar -czvf archive.tar.gz $HOME/archive/

How to extract a tar.gz compressed archive on Linux

tar -xvf file.tar.gz
tar -xzvf file.tar.gz
tar -xzvf projects.tar.gz

Her's how you can extract files in a given directory such as /tmp/

tar -xzvf archive.tar.gz -C /tmp/

Understanding what the tar command options are:

-c Create a new archive
-x Extract files from an archive
-t List the contents of an archive
-v Verbose output
-f file.tar.gz Use archive file
-C DIR Change to DIR before performing any operations
-z Filter the archive through gzip i.e. compress or decompress archive

This article was last modified: Jan. 18, 2022, 4:05 p.m.


Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.


Hacker News