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 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
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:
Extracting a .tar archive
We can extract files from the archive using the given command.
tar xvf new_archive.tar
Output:
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 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:
Extracting a .tar.gz archive
We can extract files from the archive using the given command.
tar xzf second_archive.tar.gz
Output
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:
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:
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:
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:
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:
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:
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:
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: