Tar Command in Linux – Options + Examples

Last updated: April 25, 2023

You may have downloaded or a backup file with .tar.gz or tgz archive files. You need to know how to unzip those files.

In this tutorial, we learn about tar command in Linux to create compressed or uncompressed archive files. We will also go through how to extract files using the tar command.


  • A Linux system.
  • Basic command line knowledge.
  • A good smile to learn.

What is tar in Linux

The tar command is used in Linux to create compressed or uncompressed archives containing a set of files or a single file. The tar utility can create .tar archive (uncompressed) and compress it using compression algorithms such as xz, gzip, and bzip2.

The compressed archive file or called tarballs will have extensions such as .tar.gz (gzip compression) and .tar.bz2 (bzip2 compression).

How to use tar command

Tar command is generally used to compress files and directories into a single archive. Tar can use different compression algorithms to create tarballs which having extensions such .tar.gz and .tar.bz2.

The basic syntax of tar command:

tar [options][archive-file] [file or directory to be archived]

Some of the important tar options are:

-c Create an archive file
-vVerbose mode, show the progress when creating the archive file
-xExtract an archive file
-zTell tar command to compress the archive using gzip compression algorithm
-jTell tar command to compress the archive using bzip2 compression algorithm
-tDisplay files in the archive file
-rAdds file or directory to a tar file
-f Can specify an archive filename

To compress a directory or a file in Linux, use the following syntax:

tar -czvf new-archive-file.tar.gz /path/to/directory-or-the-file

For example to compress the directory named Projects in the current directory and save it with the name project-archive-tar.gz, type:

tar -czvf project-archive.tar.gz Projects
tar compress directory in Linux

If you get a message saying "tar: Removing leading `/' from member names" it's only information you can use -P option or remove the absolute path. Example: tar -czvf new-archive-file.tar.gz -P /path/to/directory-or-the-file

Tar Command Examples

Let's check more into the Linux Tar command and its usage with examples.

Create an Uncompressed tar file

To create an archive file without any compression using -cvf option.

tar -cvf move.tar file.txt file2.txt file2.txt

This creates tar files, it is used to just bundle a bunch of files or directories

Compress a Whole Directory

You can tell the tar command to create archive files with compression using various algorithms. You need to use -z option for gzip and -j option for bzip2 algorithm.

For example to compress a directory named projects using gzip and save the file as projectbackup.tar.gz, type:

tar -czvf projectbackup.tar.gz /home/foo/documents 

The above command will create an archive file named projectbackup.tar.gz in the current directory.

You can use replace -z with -j option to compress using bzip2 algorithm.

Compress multiple directories

The following command compress directories to an archive file named compressed-file.tar.gz in the current directory:

tar -czvf compressed-file.tar.gz /path/to/directory1 /path/to/directory2

Compress Multiple Files

To compress multiple files into one archive file, type:

tar -czvf projectbackup.tar.gz doc1.txt doc2.txt doc3.pdf

Exclude Directory when creating Archive File

When compressing you can exclude specific directories or files using --exclude switch.

tar -czvf projectbackup.tar.gz Projects --exclude=/home/linuxopsys/CSS --exclude=  /home/linuxopsys/data  --exclude=*.jpg

The exclude switch also accepts patterns such as --exclude=*.jpg.

Extract tar.gz compressed file

To extract or untar archive file named compressed-file.tar.gz, run the following command:

tar -xzvf compressed-file.tar.gz 

The content of the archive file will be extracted to the current directory.

Extract files to a specific directory

Above we saw the content of the archive is extracted to the current directory. In case you want to extract the archive to a specific directory use -C option.

tar -xzvf compressed-file.tar.gz -C /home/linuxopsys/documents

List the contents of Archive file

What about you want to see the archive file contents? It's possible to see the content of a tar file without extracting it.

The following command list the contents of the tar archive file named compressed-file.tar.gz:

tar -tvf compressed-file.tar.gz

To search specific filename in the archive file, type:

tar -tvf compressed-file.tar.gz filename 

Extract only specific file from archive file

After listing the archive file content, if you feel to extract specific files, use the following command:

tar -xzvf compressed-file.tar.gz "file1" "file2"

You may also use patterns to match and extract a specific set of files:

tar -xzvf compressed-file.tar.gz --wildcards “*.jpg”

Add files to existing tar file

Use option -r to add files to an existing archive file. The archive file should be .tar files.

tar -rvf compressed-file.tar newfile

Note: It's not possible to add files to a compressed archive file. The workaround would be to gunzip first, then add files to .tar file, and then gzip that file.


In this tutorial, we learned how to use tar command in Linux. We have explained tar options with usable examples. For more information please use tar man page or simply type man tar from the command line.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment