Last updated on December 12th, 2019 at 03:28 pm

If you are working on Linux server you always need command to create a tar.gz file or unzip the tar.gz file to zip unzip project on your server or to take a backup of your file. So here a simple command you can use to create a tar.gz files in the server using the command line.

Creating a tar.gz file in Linux command line.

Here is a simple procedure to create a tar file in Linux.

  1. Open the terminal application in Linux
  2. Run tar command to create an archived named file.tar.gz for given directory name by running: tar -czvf file.tar.gz /path_of_directory
  3. Verify tar.gz file using the ls command and tar command
tar -czvf zip_name.tar.gz /path

Unzip the tar.gz file in Linux command line.

Now if you want to extract tar.gz file in your server run below command

tar -xzvf file.tar.gz

or if you want to extract an archive instead of creating one.

You may want to extract the contents of the archive to a specific directory. You can do so by appending the -C switch to the end of the command. For example, the following command will extract the contents of the archive.tar.gz file to the /temp directory.

tar -xzvf file.tar.gz -C /temp

Understanding tar command options

tar command option Description
-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

Hope this tip helps you.