Tar Command in Linux ( Compress and Decompress files)

Compressing files saves disk space, network bandwidth. You should be familiar with .zip and .rar files in Windows. Here we will cover Linux tar command in detail along with practical examples.

Linux tar command

tar (tape archive) is a Linux command which you can use compress and decompress files with extension tar, tar.gz, .tgz or tar.bz2. These formats, although not as well known as .rar or .zip, are better because they have a better compression rate and do not affect the permissions of these files.

This command is included in all Linux distributions.  Below is the basic syntax of tar

:~$ tar [OPTIONS] [FILE...]

However, some options modify the execution of the command. Some of them are the following:

Option Description
 -j Compress the file using bzip2.
 -v Verbose. It shows the progress of the operation on the screen. Either compress or decompress.
 -f Specifies the name of the file to be generated.
-z Compress the file using bzip.
-x Decompress the file.
-C Decompress the file in a different folder than where the prompt is located.
-c Create a new .tar file
-t Used to view the contents of the file.

These are usually the most commonly used options in the tar command.

When to use Tar Command?

Some of the situations where it is convenient to use the tar command are the following:

  • Compressing large files in Linux environments.
  • Save hard disk space because compression will make them take up less space.
  • Transfer multiple folders more quickly and efficiently.
  • Decompressing files from the terminal makes it faster than from the graphical user interface.
  • Thanks to the tar command it is possible to make very large backups efficiently.

Why use Tar Command?

The tar command is very efficient in managing system resources. This allows you to have better compression rates than the rest of the most known formats.

  • tar is included in all Linux distributions because it is part of GNU. This means that as soon as the system is installed, it is ready to be used without any problems and third party programs.
  • tar does not alter the attributes and permissions of the files. This feature is essential for the installation of web applications and other scripts.
  • tar drastically reduces the size of the files, which saves space. Very useful in small servers.

How to use the tar command?

Now, let’s see how to use it. Below are some of the practical examples o tar command covering most of the options.

Creating a tar file

To create a tar archive on Linux, simply use the -cvf options, specify an output name, and tell the command which files or folders to compress.

:~$ tar -cvf [outputfile.tar] [FILE-OR-FOLDER1] [FILE-OR-FOLDER2]

For example, if we want to compress a folder called audio that is in the personal folder it would look like this:

:~$ tar -cvf audiofolder.tar /home/user/audio

Remember that you can compress several files and folders. In this case, use one folder. For example:

:~$ tar -cvf audiofiles.tar /home/user/audio/audio1.mp3 /home/user/audio/audio2.mp3

Tar is a very good format, but there is a better one.

Creating a tar.gz file with the tar command

If you want to add more compression to the tar archive, then you should use gzip. That is, create a tar.gz file

This is quite easy to do, just add the -z option. Then, the command looks like this

~$ tar -cvzf [outputfile.tar.gz] [FILE-OR-FOLDER1] [FILE-OR-FOLDER2]

As with the tar format, you can add multiple files or folders. For example:

:~$ tar -cvzf audiofiles.tar.gz /home/user/audio/audio1.mp3 /home/user/audio/audio2.mp3

This format is more efficient than tar, but keep in mind that it will take a little longer to process.

More compression? create a tbz  or tar.bz2 file

If you want to increase the compression rate, it is convenient to use the tar.bz format. The only problem of using it is that it consumes many more computer resources and takes longer.

Like the two previous examples, it is enough to add an option. In this case, it is the -j. Remember to remove the option -z.

:~$ tar -cvjf [outputfile.tar.bz] [FILE-OR-FOLDER1] [FILE-OR-FOLDER2]

Note that tbz and tar.bz are equivalent. You can use either of them and the results will be the same.

Adding a file to the existing tar archive

If a tarball already exists and you forgot to include a file for whatever reason, do not worry you can do it anyway.

To do so, you have to run the following command:

:~$ tar --append --file=[tar-file] [file-to-add]

For example, if you wanted to add a file called image.jpg to a tar file named file, the command would look like this:

:~$ tar --append --file=file.tar image.jpg

It works if instead of the name, you specify the full path of the files.

Decompress files with the tar command

I have already shown you how to compress files and create a tar, tar.bz2 or tar.gz file, but now comes the reverse process. You have to decompress them.

If it is a tar file, you can decompress it with the following command:

:~$ tar -xvf tarfile.tar

However, you can specify the folder where you want it to be unpacked with the option -C.

~$ tar -xvf tarfile.tar -C [destination-folder]

In the case of tar.gz files you have to add option z.

:~$ tar -xvzf tarfile.tar.gz

And for tar.bz2 files just add the option j.

:~$ tar -xvjf tarfile.tar.bz2

In both cases, you can add the option -C and specify in which folder you want them to be decompressed like the first example.

List the contents of the tar, tar.gz and tar.bz2 files

Before unpacking the files, you may want to examine the contents of the files. This helps prevent errors and unwanted surprises.

In the case of tar files, use the following command:

:~$ tar -tvf tarfile.tar

This command will also work with tar.gz and tar.bz2 files.

Conclusion

Mastering the tar command is very important in Linux. Especially if you use it to run a server or are a backend developer. As you have seen in this post, this command is used to compress and decompress tar, tar.gz and tar.bz2 files which are very efficient and with a great compression rate.

On the other hand, this command has options that make it even more capable. It is included in all Linux distributions and makes it easy to learn.

Now it is your turn to use it and show what you can do with it.

Scroll to Top