Tar Command in Linux: How to Compress & Extract File with Tar

General Linux Topic

March 3, 2023 by Ashley

tar command in Linux

As a Linux/Unix system administrator, you must be familiar with tar as the most used command to create compressed archive files. It is the short form of tape archive that is used to cope with tape driver backup and is pre-installed in Linux as a command utility. Examples of this tutorial help you to learn how tar command in Linux compress and extract “.xz”, “gz”, “.tbz”, and “.bz2” extensions. Obviously, compressed folders require less storage to transfer between systems. So, it is a good idea to compress large folders and files into an archive file.

After buying Linux VPS, you need to know some essential commands to manage files. Tar is one of them and in the end, you will be able to extract, create, modify, and list tar archive files. As a compressed file, an archived file contains one or more files that are grouped together for easier storage and transfer.

Compress & Extract File with Tar Command in Linux

Previously on the OperaVPS blog, you learned How To Unzip Files Using Command On Linux. Join us with this guide to learn how to create archived files with the help of tar command examples.

The basic syntax of tar command

To tar a folder in Linux, you will use a command kike below:

$ tar [options][archive-file] [file or dir to be archived]

To get familiar with this syntax and go through the examples of this tutorial, look at the parameters below to see what options tar command provides:

-c: Creates an archive tar file.

-x:  This extracts archive file.

-f: Specifies the filename of the archive file.

-v: Prints verbose information for any tar operation on the terminal.

-t:  This option lists all the files inside an archive file.

-u: Archives a file and then adds it to an existing archive file.

-r:  This option updates a file or directory located inside a .tar file

-z: This creates a tar file using gzip compression

-j: Create an archive file using the bzip2 compression

-W: Verifies an archive file.

-A:  This option concatenates the archive files.

-wildcards: This specifies patterns in the UNIX tar command.

-exclude: It excludes files and directories when creating the archive.

-delete: This option removes files and directories from the archive.

Linux tar Command Examples

You can build archive files with the tar utility using a variety of compression methods, including xz, gzip, and bzip2. It is standard practice to append the compression suffix to the archive file. For instance, the file should have the.tar.gz suffix when using gzip compression (with the -z option).

Create Archive File

To create a tar archive file, you can run a command like the following example.

# tar -cvf operavps.tar /home/operavps/

If you are interested in checking what each option used in the tar command says, have a look at the below:

c – creates a fresh.tar archive file.

v – Show the.tar file’s progress in great detail.

f – The archive file’s file name type.

Create tar.gz File

As you know, the “tar” command supports the gzip compression format having “tar.gz”, “tgz”, and “tar.gzip” extensions. For example, to create a compressed tar.gz archive file in Linux, run:

tar cvzf Operavideos.tar.gz /home/Operavideos

Since the tar.gz and tgz are the same, you can also type:

tar cvzf Operavideos.tgz /home/Operavideos

Uncompress tar.gz File

Tar command in Linux is also used to uncompress tar.gz archive files. So, you just need to use the command below to uncompress the tar.gz archive file in Linux:

# tar -xvf operavps.tar.gz

To untar in different directories, use option -C and the directory path.

List Content of tar File

You can run the command below to list the content of the tar archive file in Linux.

# tar -tvf operavps.tar

Create tar.bz2 File in Linux

Tar command in Linux helps you create a highly compressed tar archived file. Bz2 is a feature that compresses data and produces an archive file that is smaller than gzip. Compared to gzip, which takes less time, bz2 compression takes longer to compress and decompress files.

For example, when you want to create a Phpfiles-org.tar.bz2 file in Linux, type:

# tar cvfj Phpfiles-org.tar.bz2 /home/php


# tar cvfj Phpfiles-org.tar.tbz /home/php


# tar cvfj Phpfiles-org.tar.tb2 /home/php

Uncompress tar.bz2 File

You can easily use the following command when you need to uncompress the highly compressed tar.bz2 file in Linux.

# tar -xvf operavideos.tar.bz2

Untar Single file from tar File

A tar command in Linux can help you to extract a single file. Look at the below example where the cleanfiles.sh will be extracted from cleanfiles.sh.tar.

# tar -xvf cleanfiles.sh.tar cleanfiles.sh


# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh

Exclude Directories and Files

While creating a tar.gz file in Linux, you can exclude files and directories. To do this, run:

# tar --exclude='file1.txt' -zcvf backup.tar.gz /home/operavps
# tar --exclude='/home/operavps/uploads' -zcvf backup.tar.gz /home/operavps

Verify the tar, tar.gz, and tar.bz2 Sizes

Tar command in Linux enables you to check the size of the tar, tar.gz, and tar.bz2 Archive File. Run the command below to view the archive file’s size in Kilobytes (KB).

# tar -czf - operavps.tar | wc -c
# tar -czf – Operavideos.tar.gz | wc -c
# tar -czf - Phpfiles-org.tar.bz2 | wc -c

Untar tar File

Use the following command to untar/extract a tar archive file in Linux.

## Untar files in Current Directory ##
# tar -xvf public_html.tar

To untar in a different directory, use option -C.

## Untar files in specified Directory ##
# tar -xvf public_html.tar -C /home/public_html/videos/

Extract Group of Files in Linux

Using Wildcard, you can extract a group of files. Check the following example to see how you can use this command to extract a group of all .php files from a tar, tar.gz, and tar.bz2 archive file.

# tar -xvf Phpfiles-org.tar --wildcards '*.php'
# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'
# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'

Delete File/Directory from tar

Tar command in Linux helps you to remove files and directories from tar archive. To do this, just run the command below and remove them from the already created tar file.

# tar --delete -f backup.tar.gz file1.txt
# tar --delete -f backup.tar.gz '/home/operavps/uploads'

Untar Multiple files from tar, tar.gz, and tar.bz2 File

Using the examples below enables you to untar/extract your considered files from the tar, tar.gz, and tar.bz2 archive files.

# tar -xvf operavps.tar "operafileA" "fileB" 
# tar -zxvf Operavideos.tar.gz "operafileA" "fileB" 
# tar -jxvf Phpfiles-org.tar.bz2 "operafileA" "operafileB"

Extraction of a Certain File Extension in tar

Using the tar command in Linux helps you to extract specific File extensions in the tar archive. To do this, run:

# tar -xvf backup.tar.gz --wildcards '*.png'

The above command extracts files with the specific extension .png from the tar archive file.

List Content tar.gz File

To list the content of the tar.gz file in Linux, type:

# tar -tvf staging.operavps.com.tar.gz

Add Files/Directories to tar File

To add files or directories to the existing tar archive files in Linux, use the example below in which the file xyz.txt and directory php.

# tar -rvf operavps.tar xyz.txt
# tar -rvf operavps.tar php

Untar Single file from tar.gz File

To extract a single file from the tar.gz archive file in Linux, use the following command as follows.

# tar -zxvf operavpsbackup.tar.gz operavpsbackup.xml


# tar --extract --file=operavpsbackup.tar.gz operavpstbackup.xml

List Content tar.bz2 File

To list the content of the tar.bz2 archive file in Linux, type:

# tar -tvf Phpfiles-org.tar.bz2

Add Files/Directories to tar.gz and tar.bz2 Files

Since the tar command has no option to add files or directories to an existing compressed tar.gz and tar.bz2 archive file, the below error will be shown when you try to do this.

# tar -rvf operavideos.tar.gz xyz.txt

# tar -rvf Phpfiles-org.tar.bz2 xyz.txt

Add Files or Directories to tar.gz and tar.bz2 Files

Untar Single file from tar.bz2 File

Ta command in Linux is also used to untar a single file from tar.bz2 File. To do this, run:

# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php


# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php

The above commands extract index.php from the file Phpfiles-org.tar.bz2.

View a tar File

You can use the following command to let all the tar files and folders in Linux be splayed in the terminal:

$ tar -tf operavps.tar.gz

Verify tar, tar.gz, and tar.bz2 File

To verify any tar or compressed archived file in Linux, run the command below and consider that you cannot do verification on a compressed archive file.

# tar tvfW operavps.tar


In this article, you learned how to use the tar command in Linux to compress, uncompress, untar, create, and extract files with tar. It offers a variety of command options to assist you in managing archive files produced by several algorithms, including gzip, xz, and bzip2. Reviewing all the practical tar command examples in this article helps you to be an expert in using tar as an essential command. But if you know any more commands, share them with us and our Linux fans.


​ ​ ​ ​

About the Author Ashley

Leave a Reply

Your email address will not be published. Required fields are marked *