If you’re a Linux user, you’re likely familiar with the tar command. Tar stands for “tape archive,” and it’s a popular command in Linux used for creating and extracting archives in Linux. In this article, we’ll explore everything you need to know about the tar command in Linux, from the basics of creating and extracting archives to more advanced features like compression and adding files to an existing archive.
Tar Command in Linux
As mentioned above, tar is a command-line tool used in Linux for creating, extracting, and manipulating archive files. There are two versions of Tar; BSD tar and GNU tar, which have functional differences.
BSD tar was the first version of Tar, and it was created in the 1970s. It was designed to work on the BSD operating system and has been ported to other Unix-like operating systems. BSD tar is known for its simplicity and reliability.
GNU tar is the other version of Tar, and it was created in the 1980s. GNU tar was designed to be more powerful than BSD tar, and it includes features like compression, multiple volume support, and enhanced file format support. GNU tar is also more flexible than BSD tar and supports more command-line options.
Most Linux systems come with GNU tar pre-installed by default, and it is the version of Tar that is most commonly used. However, some older systems may still use BSD tar. It’s important to be aware of which version of Tar is being used on a system because the syntax and command-line options may differ between the two versions
Basic Tar Command Syntax in Linux
The basic syntax for using the tar command is as follows:
tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]
The syntax for using the Tar command consists of several elements:
- OPERATION – This is the first argument and specifies the operation to be performed. There are three primary operations that can be used with Tar: create (-c), extract (-x), and list (-t).
- Create (-c): Creates a new tar archive containing the specified files or directories.
- Extract (-x): Extracts files from a tar archive to the current directory or a specified directory.
- List (-t): Lists the contents of a tar archive without extracting its files.
- OPTIONS – This is the second argument and specifies any optional parameters that modify the behavior of the operation. Some commonly used options include:
- Verbose (-v): Provides additional information about the operation, such as the name of each file as it is processed.
- File (-f): Specifies the name and location of the archive file to be created or extracted.
- Compression (-z or -j): Specifies the type of compression used for the archive file. -z is for gzip, and -j is for bzip2.
- ARCHIVE_NAME – This is the third argument and specifies the name of the archive file to be created or extracted.
- FILE_NAME(s) – This is the fourth argument and specifies the files or directories to be included in the archive. This argument is not always required, depending on the operation being performed. If no files or directories are specified, the operation will apply to the entire archive.
To use the Tar command, you would enter the operation and any options you want to use, followed by the name of the archive file, and then the names of any files or directories you want to include in the archive. The order of these arguments can vary depending on the operation and options being used.
How to Create Archives with Tar Command in Linux
To create a new tar archive, use the -c
option followed by the -f
option to specify the name of the archive file and the list of files or directories to be included in the archive. For example, to create an archive file called myarchive.tar
that includes all files in the current directory, use the following command:
tar -cf myarchive.tar .
In this example, the dot (.) represents the current directory, and all files in the directory will be included in the archive. You can also specify the list of files or directories to be included in the archive. For example, to include only the files file1.txt
and file2.txt
, use the following command:
tar -cf myarchive.tar file1.txt file2.txt
How to Compress Archive With Tar Command in Linux
You can compress an archive as you create it by adding a compression flag (-z
for gzip or -j
for bzip2) to the tar
command:
tar -czvf archive.tar.gz file1 file2 dir1
tar -cjvf archive.tar.bz2 file1 file2 dir1
In the first example, archive.tar.gz
is a gzip-compressed archive, and in the second example, archive.tar.bz2
is a bzip2-compressed archive.
The resulting archive file will be compressed using gzip compression & bzip2-compression, which will reduce the file size and make it easier to transfer or store.
How to List Tar Archives in Linux
To list the contents of a tar archive without extracting the files, use the -t
option followed by the -f
option and the name of the archive file. For example, to list the contents of the myarchive.tar
archive, use the following command:
tar -tf myarchive.tar
Output
file1.txt
file2.txt
subdir/
subdir/file3.txt
This output indicates that the archive myarchive.tar
contains two files named file1.txt
and file2.txt
, as well as a directory named subdir
. The subdir
directory contains one file named file3.txt
.
How to Extract Tar Archive in Linux
To extract the files from a tar archive, use the -x
option followed by the -f
option and the name of the archive file. For example, to extract the contents of the myarchive.tar
archive in the current directory, use the following command:
tar -xf myarchive.tar
This will extract all files and directories included in the archive into the current directory.
How to Extract Tar Archive in a Different Directory in Linux
If you want to extract the files from a tar archive into a different directory, use the -C
option followed by the directory path and the -x
option, followed by the -f
option and the name of the archive file. For example, to extract the contents of the myarchive.tar
archive into a directory called mydir
, use the following command:
tar -xf myarchive.tar -C mydir
This will extract all files and directories included in the archive into the mydir
directory.
How to Extract Tar Gz Compressed and Tar Bz2 Compressed Archives in Linux
To extract the contents of a tar archive that is compressed using gzip or bzip2 compression, use the -x
option followed by the -f
option and the name of the compressed archive file. For example, to extract the contents of the myarchive.tar.gz
archive in the current directory, use the following command:
tar -xzf myarchive.tar.gz
Similarly, to extract the contents of the myarchive.tar.bz2
archive in the current directory, use the following command:
tar -xjf myarchive.tar.bz2
How to Extract Specific Files from a Tar Archive in Linux
To extract specific files or directories from a tar archive, use the -x
option followed by the -f
option and the name of the archive file, followed by the list of files or directories to be extracted. For example, to extract only the file file1.txt
from the myarchive.tar
archive, use the following command:
tar -xf myarchive.tar file1.txt
In this example, only the file1.txt
file will be extracted from the archive. You can also specify directories to be extracted. For example, to extract the directory mydir
from the myarchive.tar
archive, use the following command:
tar -xf myarchive.tar mydir/
How to Extract Files From a Tar Archive Using Wildcard in Linux
You can use wildcards to extract multiple files or directories that match a certain pattern from a tar archive. For example, to extract all files with the extension .txt
from the myarchive.tar
archive, use the following command:
tar -xf myarchive.tar --wildcards '*.txt'
In this example, the --wildcards
option is used to specify the pattern to match, which is *.txt
. This will extract all files with the extension .txt
from the archive.
How to Add Files to Existing Tar Archive in Linux
To add files to an existing tar archive, use the -r
option followed by the -f
option and the name of the archive file, followed by the list of files to be added. For example, to add the file newfile.txt
to the myarchive.tar
archive, use the following command:
tar -rf myarchive.tar newfile.txt
This will add the file newfile.txt
to the existing archive.
How to Remove Files From a Tar Archive in Linux
To remove files from an existing tar archive, use the --delete
option followed by the -f
option and the name of the archive file, followed by the list of files to be removed. For example, to remove the file file1.txt
from the myarchive.tar
archive, use the following command:
tar -f myarchive.tar --delete file1.txt
This will remove the file file1.txt
from the existing archive.
Conclusion
The tar command is a versatile tool for creating, extracting, and manipulating archives in Linux. With its various options and use cases, it can be used to simplify file management and transfer. This guide has covered the basic syntax of the tar command and provided examples for creating, extracting, and manipulating archives in various ways. By mastering the tar command, you can enhance your productivity and efficiency in Linux.
Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.