How to use the Tar command in Linux to compress and extract files

One of the most commonly used commands to compress and extract files is ‘tar’. The tar tool, which stands for ‘tape archive’, is one of the most important utilities for system administrators in the Unix/Linux world. The tar command creates .tar.gz or .tgz files which are also called ‘tarballs’.

The tar command is not so straightforward to use for beginners. The complexity stems from the many settings and options that you can use with tar. The basic form is the simplest to use.

In the article, you will learn how to use the tar command in order to extract and create archive files, display and verify the content of a file as well as how to add directories or files to archive, etc…

Creating a tar archive file

Let us first create a tar archive file for a specific directory. The following command will compress an entire folder net2_dmin/Downloads/ as well as its sub-folders, or in other words, it will perform a recursive compression on the whole directory :

tar -cvzf new_compressed_file.tar.gz net2_dmin/Downloads/

How to tar a file in linux | Linux tar directory | Linux tar folder

Here’s an explanation of the switches used in the example above:

  • -c: Creates an archive.
  • -v: Displays progress while performing the archive creation. It is what is called “verbose” mode. The v, though it is optional, might come in handy.
  • -z : Compresses the archive using gzip.
  • -f: Enables you to specify the archive file.

Note that the extension .gz is also similar to the extension .tgz .

Compressing multiple directories or files

the tar command can be used to compress multiple single files or multiple directories or a mix of both since in Linux, a directory is also a file.You would just need to provide the list of directories or files in cascade as shown in the example below :

To compress the directories home/dir1, home/dir1, home/dir3 and home/dir4/singlefile.mp3, just issue the command below :

tar -czvf archive.tar.gz home/dir1 home/dir1 home/dir3 home/dir4/singlefile.mp3

Listing the content of a tar archive file

In order to display a list of the content of a tar archive file, you would just need to run the command below with the option t :

tar -tvf new_compressed_file.tar.gz

Linux tar gz

This is the same command for a .tar file .

Excluding Specific files from being compressed

In some circumstances, you may want to exclude certain files or directories from being compressed when you want to run tar on a parent directory.This can be done by using the –exclude switch for each file or directory to wish to exclude.

For instance you would like to compress the directory ‘/home/folder’ but you do not want to include the folder ‘/home/folder/examples’ and the file ‘/home/folder/single_file.txt ‘, here is what needs to be done :

tar -czvf file_archived.tar.gz /home/folder –exclude=/home/folder/examples –exclude=/home/folder/single_file.txt

There is a lot more you can perform with the powerful –exclude switch since it can also accept patterns.For instance it is possible to archive a whole folder and at the same time exclude all .txt files. This can be achieved via the example command below :

tar -czvf archived_file.tar.gz /home/flder –exclude=*.txt

Creating tar.bz2 archive file

It is possible to compress files to even smaller sizes using the bz2 feature. Though it takes more time to compress, the bz2 enabled compression yields a more compressed file when compared to the usual gzip in terms of size.This also applies to decompression with bz2.

For instance in order to create a bz2 enabled compression on a directory, execute the command below :

tar -cvfj file_compressed.tar.bz2 /home/net2_admin/Downloads

Notice the ‘j’ in the main switch above.

Adding files to existing tar archive file

This powerful feature allows you to add files or even folders to existing tar archive files.

For instance we would like to add the file file2.txt to existing tar file file1.tar archive file. This can be done as follows using the -r option:

tar -rvf file1.tar file2.txt

NOTE: for existing ‘.gz’ and ‘.bz2’ tar archive files, it is not possible to add files.

Read: How to use the Gzip command in Linux

Extracting archive files

In case you want to uncompress an already tarred file, you would be able to do so with the tar command as well. The command below will extract the content of a file to the current directory :

tar -xzvf new_compressed_file.tar.gz

How to untar tar.gz file

If however you would like the uncompressed file to be created in a different directory, you just need to append the -C switch as follows :

tar -xzvf new_compressed_file.tar.gz -C net2_admin/

If the file was compressed using bzip2, just replace the ‘z’ in the commands above with a ‘j’.

Extracting a single file from a tar/bz2 File

In order to extract a specific file from a tar file, you would need to use the following command :

tar -xvf main_tar_file.tar single_file.txt

OR

tar –extract –file=main_tar_file.tar single_file.txt

For a bz2 file, just add a ‘j’ to the switch above, so that it becomes -jxvf .

It is also possible to extract a set of files, just write them in cascade as :

tar -xvf main_tar_file.tar single_file1 single_file2 single_file3

For a .tar.gz , just use the switch -zxvf and for a .tar.bz2, use -jxvf .

Extracting files using wildcards

If all the files that you would want to extract have a common name pattern, for instance, they all start with the word ‘file’, then it is possible to carry out a group extraction using wildcards. This goes as follows:

tar -xvf main_file.tar –wildcards ‘file*’

As usual for a bz2 and .tar.gz, use the switches as mentioned in previous sections.

Size of the tar archive file

In order to know the size of a tar archive file (.tar.gz or .bz2), you would need to use the command below :

tar -czf new_compressed_file.tar | wc -c

For more information on the tar command, you can open the man page.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

ziad nahdy

Ziad Nahdy, fan of open source and programming languages. He is a technical writer, blogger and Linux enthusiast. He loves to read and help others with their problems. He is addicted to open source software but he also loves other technology related subjects.

This Post Has 4 Comments

  1. Experienced Admin

    tar stands for tape archive not tap archive (you’re missing an e).

    1. admin

      Thank you – corrected .

  2. Kristian

    I think an error crept in the wildcards part. You wrote:
    > they all start with the word ‘file’
    But then, the presented command was
    > tar -xvf main_file.tar –wildcards ‘*file’

    When I understand the command correctly, this command would extract all files *ending* with the word ‘file’. I think the correct command would be
    > tar -xvf main_file.tar –wildcards ‘file*’

    1. admin

      Oh thank you very much ! it has now been corrected . Have a great day.

Leave a Reply