Signup/Sign In
PUBLISHED ON: JANUARY 19, 2022

Tar Command in Linux (Create and Extract Archives)

Tar

The GNU tar tool bundled with Linux distributions features inbuilt compression. It can generate a .tar archive and then compress it with gzip or bzip2 compression in a single operation. That's why the resultant file is a .tar.gz file or .tar.bz2 file.

Compress an Entire Directory or a Single File

Use the following command to compress a whole directory or a single file on Linux. It'll also compress every other directory within a directory you specify–in other words; it operates recursively.

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

Here's what those switches genuinely mean:

  • -c: Create an archive.

  • -z: Compress the archive using gzip.

  • -v: Display progress in the terminal while building the archive, often known as "verbose" mode. The v is usually optional in these instructions, but it's beneficial.

  • -f: Allows you to provide the filename of the archive.

Imagine you have a directory called "stuff" in the current directory, and you want to save it to a file named archive.tar.gz. You'd execute the following command:

tar -czvf archive.tar.gz things

Or, let's assume there's a directory at /usr/local/something on the present system, and you want to compress it to a file called archive.tar.gz. You'd execute the following command:

tar -czvf archive.tar.gz /usr/local/something

Compress Multiple Directories or Files at Once

While tar is usually used to compress a single directory, you might also use it to compress multiple directories, numerous individual files, or both. Just offer a list of files or folders instead of a single one. For example, let's imagine you wish to compress the /home/ubuntu/Downloads directory, the /usr/local/stuff directory, and the /home/ubuntu/Documents/notes.txt file. You'd execute the following command:

tar -czvf archive.tar.gz /home/ubuntu/Downloads /usr/local/stuff /home/ubuntu/Documents/notes.txt

Just specify as many folders or files as you wish to back up.

Exclude Directories and Files

You may desire to compress a whole directory in rare circumstances but not include particular files and folders. You may do so by inserting a —exclude option for each guide or file you wish to exclude.

For example, let’s imagine you want to compress /home/ubuntu, but you don’t want to compress the /home/ubuntu/Downloads and /home/ubuntu/.cache folders. Here’s how you’d do it:

tar -czvf archive.tar.gz /home/ubuntu —exclude=/home/ubuntu/Downloads —exclude=/home/ubuntu/.cache

The —exclude switch is powerful. It doesn't take names of folders and files–it takes patterns. There's a lot more you can do with it. For example, you might archive a complete directory and exclude all .mp4 files using the following command:

tar -czvf archive.tar.gz /home/ubuntu —exclude=*.mp4

Use bzip2 Compression Instead.

While gzip compression is usually used to generate .tar.gz or .tgz files, tar also supports bzip2 compression. This enables you to produce bzip2-compressed files, frequently termed .tar.bz2, .tar.bz, or .tbz files. To do so, replace the -z for gzip in the scripts given with a -j for bzip2.


Gzip is quicker, but it often compresses a little less, so you receive a slightly bigger file. Bzip2 is slower, but it crams a little more, so you get a somewhat smaller file. Gzip is also increasingly prevalent, with some stripped-down Linux systems offering gzip support by default, but not bzip2 support. In general, however, gzip and bzip2 are virtually identical, and both will perform similarly.

For example, instead of the first example we gave for compressing the things directory, you'd execute the following command:

tar -cjvf archive.tar.bz2 things

Extract an Archive

Once you have an archive, you may extract it using the tar command. The following command will remove the contents of archive.tar.gz to the current directory.

tar -xzvf archive.tar.gz

It's the same as the archive creation command we used earlier, and only the -x option substitutes the -c switch. This signals you wish to extract an archive instead of generating one.

You may choose to extract the archive's contents to a specified directory. You may do so by attaching 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 /tmp directory.

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

If the file is a bzip2-compressed file, replace the "z" in the preceding instructions with a "j".

This is the most straightforward conceivable use of the tar command. The command offers many other options, so we can't reasonably put them all here. For more details. Execute the info tar command at the shell to display the tar command's complete information page. Press the q key to leave the information page when you're done. You may also read tar's manual online.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.