Linux Server

14 Tar Command in Linux (with Examples)-Serverwala

TAR files are created and compressed to allow easy data transfer between different devices. There are many names for the tar command in Linux and Unix, including gzip, tarball, and bzip. Tar symbolizes tape archives in Linux and is commonly used to back up tape drives in Unix and Linux systems. In the following paragraphs, we will discuss tar command illustrations and how to use tar in more detail.

Tar Command Syntax examples

Tape archives are created using the Linux tar command and extracted with the Linux unzip command. As part of Linux, the tar Linux command functions as an essential tool for archiving.

One definition of an Archive file is a file that contains one or more files along with metadata. Multiple files can be gathered into one to enhance storage and portability or compressed to make storage space more efficient with the right tar.gz commands and other tar options

With Linux tar, we can create and maintain Archive files. This list of commands show you how to tar a directory in UNIX with examples, as well as compress and uncompress them by modifying the tar command using these options:

cGenerates an Archive
xExtraction of the archive
tLists or displays files within an archive
rupdates or adds files or directories to existing .tar files
uarchiving and adding to existing archives
vDisplays detailed information
jusing tbzip to filter tar archives
WVerify or check the integrity of a file
AConcatenates the archives
zzip tells tar command to use gzip to create the tar file
fCreates a file-based archive

Making a new tar Archive file

# tar -cvf (name of file).tar /home/(name of directory)

This line command tar example creates a new tar archive file, complete with the name and date for a particular directory. Note that the file and directory names and dates used here are fictional. The letters ‘tar command Linux in this tar Unix command refers to the specific function listed in the bale above – c: create a new .tar file, v: shows progress on the .tar file verbosely, and f: type of file name of the new archive file.

Extracting archive files

               $ tar xvf file.tar

These tar commands in Linux will extract specific archive files using the syntax ‘xvf’.

Creating a tar.gz archive file

# tar -cvf (name of file)-11-05-2021.tar.gz home/(name of compressed file)

$ tar cvzf file.tar.gz *.c

The option ‘z’ placed in the line tar commands will make a compressed gzip file. Note that ‘tgz’ and ‘tar.gz’ are identical while the file name is ‘file.tar.gz. If the file is an archive of the .c files, then apply the second Linux tar command.

Extracting gzip archive file

$ tar xvzf file.tar.gz

This tarball Linux command extracts files from the tar archive directly ending with ‘file.tar.gz’.

Making new compressed Linux files

$ tar cvfj file.tar.tbz example.cpp

The list shows a ‘j’ option in the tar command Linux that can be applied to make new archive files and compress them into a smaller size than the gzip file. The decompress and compress function takes longer to process.

Making new tar.bz2 archive files

# tar cvfj (name of file).tar.bz2 /home/(Directory name)

# tar cvfj (name of file).tar.tbz /home/(Directory name)

# tar cvfj (name of file).tar.tb2 /home(Directory name

The ‘j’ options compresses a tar file, and the bz2 option compresses and archive files that are smaller than gzip files. The bz2 file compression option takes even more time.

Untar specific files or directories in Linux

$ tar xvfj file.tar

$ tar xvfj file.tar -c (file/directory path)

This tar xvf line will untar a file in the directory that you specify. You need to add the ‘c’ option.

Untar single tar, tar.gz, tar.bz2 files  

# tar -xvf file

# tar –extract –file=file


# tar -zxvf filename.tar.gz directory.xml

# tar –extract –file= filename.tar.gz directory.xml


# tar -jxvf filename.tar.bz2 home/(directory name).php

# tar –extract –file= filename.tar.bz2 /home/(directory name).php

You have two options when using this command – extract a single .tar file from the archive using the affix to extract from the These commands will bring up specified tar.gz files from the tar.gz pr tar.bz2 directories.

Untar multiple files in the .tar, .tar.tbz or .tar.gz formats

$ tar xvf file.tar “fileA” “fileB”

$ tar zxvf file1.tar.gz “fileA” “fileB”

$ tar jxvf file2.tar.tbz “fileA” “fileB”

These commands extract or untar several files from the archive files at a time if they have the .tar, .tar.tbz, tar.bz2 or .tar.gz names. File A and B refer to the specific file names.

Decompress tar.gz files

# tar -xvf thumbnails-14-09-12.tar.gz

# tar -xvf (file name)-14-09-12.tar -C /home/(name of the directory)

This command decompresses files with the tar.gz name. If you need to apply this function to multiple directories, then apply the ‘c’ option. 

Decompress tar.bz2 files

# tar -xvf (file name)-14-09-12.tar.bz2

The tar.bz2 is a heavily compressed file, and decompressing it takes time and patience. The command untars file from an archive file.

Viewing all tar, tar.gz, tar.bz2 archive file content

# tar -tvf uploadprogress.tar

# tar -tvf

# tar -tvf file name-directory name.tar.bz2

To see all of the tar content you have stored in the directory, try this command. Just like the above command, this one will show you all of the current tar.gz archive content. Similarly, view all tar.bz2 with the third command.

Wildcard group file extract

# tar -xvf filename.tar –wildcards ‘*.php’

# tar -zxvf filename.tar.gz –wildcards ‘*.php’

# tar -jxvf filename.tar.bz2 –wildcards ‘*.php’

Linux understands a wildcard extracting command for extracting a group of files at a single time from tar, tar.gz, or tar.bz2 archive files. Ideally, the file should have a similar name, for example – .php.

Adding new directories or file to existing tar archive directory

# tar -rvf filename-14-09-12.tar xyz.txt

                                           # tar -rvf filename-14-09-12.tar php   

The ‘r’ option in this command lets you add a new file or directory to existing tar archive directories. This command example adds the file called ‘xyz.txt’ and a new ‘PHP’ directory to the ‘file name’.tar archive file.

Adding new directories or file to existing tar.bz2 archive directory

# tar -rvf filename-14-09-12.tar.gz xyz.txt

# tar -rvf filename.tar.bz2 xyz.txt

Leads to error notification:

“tar: This is not a tar archive”

“tar: Skipping to next header xyz.txt”

“tar: Error exit delayed from previous errors”

So far, no command adds new files or directories to a compressed existing tar.bz2 or tar.gz. If you attempt it using the above example command,  you get the resulting error shown below. 

Preventing this error

# tar tvfW (archive name)-14-09-12.tar

Using the ‘ W ‘ option, you can use the above command to verify the compressed archive or tar file. Note that no verification can be done on compressed tar.bz2 or tar.gz archive files.

Read More: Learn to use SCP Command in Linux (with Examples)


There you have it: how to tar a file in Linux examples from different angles. Combining many files into one file is possible using the tar command in Linux. In Linux, you can use gzip, tar, tar czf, tar cvzf and bzip to compress a group of files into one file. In addition to compressing files, it is also used to make sharing files over the Internet more manageable and to reduce disk space requirements. In addition to decompressing a compressed file, the tar utility can also be employed to recoup the original data in the compressed file.

Server Wala Team

We Have Provide Cloud Managment and Support(AWS, Google Cloud, Digital Ocean, All Cloud Solution Setup and Management), DevOps Support, Virtual Private Server, Dedicated Server, Unmanaged Server, SMTP Servers, Server Management (Linux&Windows), Email Marketing. More Details mail at [email protected]

Related Articles

Adblock Detected

Please consider supporting us by disabling your ad blocker