The Linux tar command is the swiss army knife of the Linux admin when it comes to archiving or distributing files. Gnu Tar archives can contain multiple files and directories. File permissions can be preserved and support multiple compression formats. The name tar stands for “Tape Archiver,” and the format is an official POSIX standard.
Tar file formats
A short introduction to tar compression levels:
No compression. Uncompressed files have the file ending .tar.
Gzip Compression The Gzip format is the most widely used compression format for tar. It is fast for creating and extracting files. Files with gzip compression normally have the file ending .tar.gz or .tgz. Below, you will find some examples of creating and extracting a tar.gz file.
Bzip2 Compression The Bzip2 format offers better compression than the Gzip format. Creating files is slower. The file ending is usually .tar.bz2.
Lzip (LZMA) Compression The Lzip compression combines the speed of Gzip with a compression level that is similar to Bzip2 (or even better). Independently of these good attributes, this format is not widely used.
Loop Compression This compress option is probably the fastest compression format for tar. It has a compression level similar to gzip and is not widely used.
The common formats are tar.gz and tar.bz2. If your goal is fast compression, then use gzip. When the archive file size is critical, then use tar.bz2.
What is the Linux tar command used for?

Here are a few common use cases of the tar command.
- Backup of Servers and Desktops.
- Document archiving.
- Software Distribution.
How to install tar?
- CentOS
Execute the following command as root user on the shell to install tar on CentOS.
yum install tar
- Ubuntu
This command will install tar on Ubuntu. The “sudo” command ensures that the apt command is run with root privileges.
sudo apt-get install tar
- Debian
The following apt command installs tar on Debian.
apt-get install tar
- Windows
The tar command is available for Windows as well. You can download it from the Gunwin project.
How to Create tar.gz Files?
Here is the tar command that has to be run on the shell. I will explain the command-line options below.
tar pczf myarchive.tar.gz /home/till/mydocuments
This command creates the archive myarchive.tar.gz, which contains the files and folders from the path /home/till/mydocuments. The command-line options explained:
[p] This option stands for "preserve", it instructs tar to store details on file owner and file permissions in the archive.
[c] Stands for create. This option is mandatory when a file is created.
[z] The z option enables gzip compression.
[f] The file option tells tar to create an archive file. Tar will send the output to stdout if this option is omitted.
Linux Tar command examples
Example 1: Backup the /etc Directory Create a backup of the /etc config directory. The backup is stored in the root folder.
tar pczvf /root/etc.tar.gz /etc
The command should be run as root to ensure that all files in /etc are included in the backup. I’ve added the [v] option in the command this time. the option stands for verbose, and it tells tar to show all file names that get added to the archive.
Example 2: Backup your /home directory. Create a backup of your home directory. The backup will be stored in a directory /backup.
tar czf /backup/myuser.tar.gz /home/myuser
Replace my user with your username. I’ve omitted the [p] switch in this command, so the permissions are not preserved.
Example 3: A file-based backup of MySQL databases The MySQL databases are stored in /var/lib/mysql on most Linux distributions. You can check that with the command:
ls /var/lib/mysql
Stop the database server from getting a consistent MySQL file backup with tar. The backup will be written to the /backup folder. 1) Create the backup folder
mkdir /backup
chmod 600 /backup
Stop MySQL, run the backup with tar and start the database server again.
service mysql stop
tar pczf /backup/mysql.tar.gz /var/lib/mysql
service mysql start
ls -lah /backup
Extract tar.gz Files
The command to extract tar.gz files is:
tar xzf myarchive.tar.gz
Use of Tar Command in Linux With Examples
If you’re a Linux system administrator, you probably know how to use the tar command, which is a powerful tool for portability. It combines data from multiple locations into one, resulting in a compressed file. The tar command combines data in the form of a single file or multiple directories. To get more information about the tar command, use the man or -help commands.
[x] The x stand for extract, it is mandatory when a tar file shall be extracted.
[z] The z option tells tar that the archive that shall be unpacked is in gzip format.
[f] This option instructs tar to read the archive content from a file, in this case the file myarchive.tar.gz.
The above tar command will silently extract that tar.gz file, it will show only error messages. If you like to see which files get extracted, then add the "v" option.
tar xzvf myarchive.tar.gz
The [v] option stands for verbose, it will show the file names while they get unpacked.
When using the tar command, you should include the -c option and specify the type of archive you want to create. This option allows you to write only the specified file. The -v option enables you to view archived files verbosely. The -f option is the name of the archive and must precede the filename. You can also use the tar command to extract specific files or multiple files. It is also possible to unzip a file to a different directory.
The Linux Tar Files Folder and Subfolders Command
Another useful option is -T. The -T option lets you specify a file name as a string. This is handy if you want to package applications. If you want to create a backup of your OS, you can use the -T option. It will then create a compressed file. The -t option will also create a tar.gz file, a tar archive compressed with Gzip.
The Linux Tar files Folder and Subfolder command allow you to extract the content of an archive. You can specify files and directories to extract by adding the -T option. This command will ignore files it doesn’t recognize. If you have a lot of files in your archive, you can use the -T option to specify which files to extract. Otherwise, tar will create a directory for all extracted files.
Besides the -r and -c options, you can also specify the name of the archive file to extract. This option does not require any arguments. If you don’t specify any options, the tar command will display the defaults. If you’re not sure what they mean, read the tar command’s documentation and use the -r option to set the permissions of files extracted.