Home Linux How to create tar.gz file in Linux using command line

How to create tar.gz file in Linux using command line


How to create tar.gz file in Linux using command line – In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from “tape archive”, as it was originally developed to write data to sequential I/O devices with no file system of their own.

How to create tar.gz file in Linux using command line

Please follow below steps to create a tar.gz file on Linux;

  • Open the terminal application in Linux
  • Run tar command to create an archived named file.tar.gz for given directory name
  • Verify tar.gz file using the ls command and tar command

tar command options

  • c = create a new tar file
  • v = verbose , display file to compress or uncompress
  • f = create the tar file with filename provided as the argument
  • z = use gzip to zip it
  • x = extract file

Example command to create tar.gz file

tar -cvzf new_tarname.tar.gz folder-you-want-to-compress

In this example, compress a folder named “public_html”, into a new tar file “scheduler.tar.gz”.

tar -cvzf public_html.tar.gz public_html

Example command to extract tar.gz file

tar -xzvf tarname-you-want-to-unzip.tar.gz

In this example, uncompressed a file named “public_html.tar.gz”, into a new tar folder “public_html”.

tar -xzvf public_html.tar.gz

How to verify compress file

Please use below command to verify compressed file

ls -l public_html.tar.gz

Example outputs:

-rw-r--r-- 1 vivek vivek 59262 Oct  25 07:23  public_html.tar.gz

list the contents of a tar or tar.gz file using the tar command;

tar -ztvf public_html.tar.gz

To Preserve permissions

tar -pcvzf tar-archive-name.tar.gz source-folder-name

Example command

tar -pcvzf public_html.tar.gz public_html

Switch the ‘c’ flag to an ‘x’ to extract (uncompress).

tar -pxvzf tar-archive-name.tar.gz

Example Command

tar -pxvzf public_html.tar.gz

If you’re using a graphical Linux desktop, you could also use the file-compression utility or file manager included with your desktop to create or extract .tar files. On Windows, you can extract and create .tar archives with the free 7-Zip utility.

Use bzip2 Compression Instead

While gzip compression is most frequently used to create .tar.gz or .tgz files, tar also supports bzip2 compression. This allows you to create bzip2-compressed files, often named .tar.bz2, .tar.bz, or .tbz files. To do so, just replace the -z for gzip in the commands here with a -j for bzip2.

Sample command for bzip2

tar -cjvf public_html.tar.bz2 public_html

Online TAR.GZ compressor

You may also like : How To Install Nginx Web Server on Ubuntu 18.04



Please enter your comment!
Please enter your name here

Exit mobile version