Knowledgebase Home / How to tar a file in Linux using command line
// View Comments //

How to tar a file in Linux using command line

The name “TAR” refers to Tape Archive files and sometimes referred to as tarball. TAR is a software utility used for collecting several files into one archive file, including videos and images, in one for easier distribution or archiving. TAR file format is used to store multiple files in one single file. This documentation shows how to tar a file in Linux using the Linux tar command line option.

Steps are as follows:

  • Open the terminal option in your cPanel interface,
  •  If you want to compress an entire directory, (Example: /home/username/public_html/) run the following command.
$ tar -czvf file.tar.gz /home/username/public_html/

Here username means cPanel username of your account and file.tar.gz means the name for your compressed file.

  • To Compress a single file, run the following command
$tar -zcvf file.tar.gz /home/username/filename
Here filename means the file which you want to compress
  • To compress multiple directories and files at once, you can use the following command formats.
$tar -czvf file.tar.gz /home/username/dir1/ /home/username/dir2/ /home/username/filename

Here dir1, dir2 means the directories which you want to compress, Filename means the files

  • To compress multiple directories you can run the TAR command in following format.
$tar -zcvf file.tar.gz dir1 dir2 dir3

-c : Create a new archive
-v : Verbose output
-f file.tar.gz : Use archive file
-z : Filter the archive through gzip

  • Exclude directories and files when using tar

Using the below syntax you can exclude certain files when creating a tar file, for example, to exclude ~/Downloads/ directory,

$ tar -zcvf file.tar.gz --exclude='Downloads' /home/username/

How do I extract an archive?

The extraction process is also as simple as compressing the files. You can use one of the following commands in order to extract a compressed file. 

$ tar -xzvf file.tar.gz
$ tar -xjvf file.tar.bz2

If you want to extract the contents of the archive into a specific directory such as /home/username/backups/ you can use one of the following commands with appropriate compressed file paths.

For example,

$ tar -xzvf my.tar.gz -C /home/username/backups/
$ tar -xjvf archive.tar.bz2 -C /home/username/backups/

-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
–exclude     : Exclude files matching PATTERN/DIR/FILENAME


Congratulations, You have learned how to tar a file in Linux using the tar command.

If you have any web hosting questions please feel free to reach out to us. We're happy to help.
Shared Hosting | Reseller Hosting | Managed WordPress Hosting | Fully Managed VPS Hosting

Our Guiding Principles

  • Provide consistent, stable, and reliable web hosting services.
  • Ensure rapid ticket response and quick resolutions to issues.
  • Never saturate or over-provision servers to ensure stability and speed for our customers.
  • Use only high-quality enterprise-class hardware to ensure minimal downtime from hardware failures.
  • Provide clear pricing with no hidden fees or gotchas.
Subscribe to comment notifications
Notify of
Inline Feedbacks
View all comments