Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The Tar Command
2.1.
Syntax of `tar` command
3.
What is an Archive file?
4.
Tar Command Applications
4.1.
Creating a .tar archive
4.2.
Extracting a .tar archive
4.3.
Creating a gzip-compressed archive
4.4.
Creating a bzip2-compressed archive
4.5.
Extracting a .tar.gz archive
4.6.
List the contents of the tar file
4.7.
Update existing tar file
4.8.
Finding a specific file in the archive
4.9.
Finding a file using grep command
4.10.
Finding file using wildcards
4.11.
Finding the archive size
4.12.
Deleting files from the archive
5.
Key implementations of ‘Tar’
6.
Frequently Asked Questions
6.1.
What is tar command Linux?
6.2.
How to extract tar in Linux?
6.3.
What is the benefit of tar in Linux?
6.4.
What is the command for tar folder?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Tar Command in Linux with Examples

Introduction

Linux ‘Tar’ Command stands for tape archive, that is mainly used to create Archive and extract the Archive files. While downloading several massive software in Linux or UNIX systems, we might have seen that the file’s extension is ‘tar.gz’. And to extract and install the software, we usually use the Tar command in Linux in the terminal. 

tar command in linux

Tar command is not just limited to extracting archives. It has a much more comprehensive application. Let’s see them in detail.

The Tar Command

Tar is among Linux and UNIX systems' most popular file management commands. It stands for “Tape Archive”. It was initially used for archiving files to tape drives. Now it is a terminal tool for archiving and compressing files and directories. It creates and manipulates various file formats, including .tar, .gz, .zip, .bz2, etc.

An archive file is a single file that contains one or more other files and/or directories. It bundles multiple files into a package for easier storage, transfer, and backup.

The Tar command in Linux can be used for various purposes like creating an archive, adding or removing files, compressing files and many more. To learn more about Linux, visit this blog.

Syntax of `tar` command

The syntax of the Tar command is as follows:

tar [options] [archive-name] [file(s) or directory]


Explanation

  • In the above code, ‘tar’ is the command itself. 
     
  • ‘Options’ are various terminal options for setting the behaviour of the Tar command. 
     
  • ‘Archive-name’ is the archive name that will be created after the execution of the above command. It should have a suitable file extension like .tar, .gz, etc. ‘File (s) or directory’ includes the name of the file or directory on which the given ‘option’ will work. To learn more about Linux commands, visit this blog.
     

We have listed various options for manipulating the Tar command’s behaviour:

Options Use
-c To create a new archive
-f To specify the archive’s name
-x To extract the archive
-t To list all files in an archive
-u To archive and add to an existing archive 
-v To display verbose information
-j To use bzip2-compression when creating or extracting an archive file
-z To use gzip-compression when creating or extracting an archive file
-W To verify an archive file
-r To update files or directories inside a .tar file
-A To concatenate all archive files
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is an Archive file?

If there is a file and its “archive” file attribute is turned on, then this file will be known as an archive file. Archive does not mean the backup, it means inactive data that an organization needs to keep.

The archive file is used in long-term data retention for legal reasons or compliance. Tape and Cloud are the most common media or technologies that are used for archiving files.

Tar Command Applications

Here are some practical implementations of the above options:

Creating a .tar archive

We have created “new_archive.tar” using the below command, which archives all jpg files.

tar cvf new_archive.tar *.jpg


Output:

creating new_archive

Extracting a .tar archive

We can extract files from the archive using the given command.

tar xvf new_archive.tar


Output:

extracting new archive

Creating a gzip-compressed archive

Using the command below, we have created “second_archive.tar.gz” for all jpg files in the present directory. It has “.tar.gz” extension.

tar czf second_archive.tar.gz *.jpg

 

Output

creating second_archive


Creating a bzip2-compressed archive

We have created “third_archive.tar.tbz” for all jpg files in the current directory using the below command.

tar cjf third_archive.tar.tbz *.jpg


Output:

creating third archive

Extracting a .tar.gz archive

We can extract files from the archive using the given command.

tar xzf second_archive.tar.gz

 

Output

extracting second archive

List the contents of the tar file

We can list all the existing files in a given tar archive using the below command. 

tar tf new_archive.tar


Output:

Output for listing all files

Update existing tar file

We can update the existing tar archive file using the below command to add files of the given names from the current directory.

tar rvf new_archive.tar *.jpg


Output:

Updating tar file

Finding a specific file in the archive

We can find any file using its name in a tar archive using the below command.

tar tf new_archive.tar "linux penguin.jpg"


Output:

Output for finding file

Finding a file using grep command

We can find any file using its name and grep command using the following command.

tar tf new_archive.tar | grep "linux penguin.jpg"


Output:

Output of finding a file using grep command

Finding file using wildcards

Using the following command, we can find a file using wildcards (symbols used to denote multiple characters in a file name like ‘*’ for multiple characters or ‘?’ for a single character).

tar tf new_archive.tar --wildcards "*.jpg"


Output:

Output for wildcard finding

Finding the archive size

We can find the size of .tar/ .tar.gz/ .tar.tbz archive files (in bytes) by using the following commands.

tar -cf - new_archive.tar | wc -c


OR

tar -czf - second_archive.tar.gz | wc -c


Output:

Output for archive size

Deleting files from the archive

We can delete files from the archive using the command below.

tar --delete -f new_archive.tar "linux penguin.jpg"


Output:

Output for deleting file

Key implementations of ‘Tar’

In this section of the article “Tar Command in Linux with Examples”, we will discuss the key implementations of the ‘tar’ command in Linux. Here are some of them are following:

  • GNU Tar: GNU Tar is one of the key implementations of tar which is a default for every Linux distribution that is basically based on a public domain implementation.
     
  • FreeBSD Tar: FreeBSD Tar is also a key implementation of tar which is a default for every Berkeley Software Distribution-based OS like Mac OS X.
     
  • Python Tarfile: Python Tarfile supports two or more tar formats like gnu, pax, and ustar.
     
  • Schily Tar: Schily Tar is a key implementation of tar that was developed in April 1997.
     
  • Solaris Tar: This key implementation of tar comes by default in the Solaris OS which is actually based on Unix V7.

Frequently Asked Questions

What is tar command Linux?

Tar is among Linux and UNIX systems' most popular file management commands. It stands for “Tape Archive”. It was initially used for archiving files to tape drives. It creates and manipulates various file formats, including .tar, .gz, .zip, .bz2, etc.

How to extract tar in Linux?

There are different commands for extracting tar in Linux, which are as follows: to extract the archive, “-x” can be used, To use bzip2-compression and gzip-compression when extracting an archive file, “-j” and “-z” can be used respectively.

What is the benefit of tar in Linux?

The “tar” command compresses efficiently and has a compression ratio of 50%. “tar” command also drastically reduces the size of package files and folders, and also it does alter the features of files and directories.

What is the command for tar folder?

To tar the folder or directory, the following command can be used: tar -cvf filename.tar /path/to/directory. In this command, “c” is the flag that signals the creation, “v” shows the process is verbose, and “f" is the last flag.

Conclusion

This article discussed the Tar command in Linux. We also saw its syntax and practical applications. Tar command’s compression ratio is about 50%. It is easily accessible on Linux machines. It is popularly used for compressing and decompressing large files in different Linux and UNIX servers.

If you found this article interesting, read our other related blogs:

You may refer to our Guided Path on Code Studios to enhance your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Previous article
Troubleshooting with Linux Logs
Next article
Compile And Run a C/C++ Code In Linux
Live masterclass