The tar command is an essential tool for Linux users, especially for those who work with a large number of files. The tar command allows you to combine multiple files into a single archive file and also to extract files from an archive. In this article, we will provide a comprehensive guide for beginners on the basics of tar command in Linux.
What is tar Command?
The tar command is short for “Tape ARchive”, which is a popular file archiving utility in Linux. Tar is used to create archive files by combining multiple files and directories into a single file. The resulting archive file can then be compressed to reduce its size and make it easier to transfer over a network. The tar command is widely used for backup purposes, as it allows you to backup multiple files and directories into a single archive file.
How to Use tar Command?
The syntax for the tar command is as follows:
1 | tar [OPTION]... [FILE]... |
The options used with the tar command are used to specify the behavior of the command. Some of the most commonly used options are:
-c
: create an archive-v
: verbose mode (display progress)-x
: extract files from an archive-f
: specify the name of the archive file-z
: compress the archive file usinggzip
-j
: compress the archive file usingbzip2
-J
: compress the archive file usingxz
-t
: viewing content of archive-O
: Display file content on stdout-r
: append file to existing archive-C
: define destination directory-W
: Verify a archive file
How to Create a tar archive
Use the following examples to create new archive file in different formats like .tar (a simple archive file), .tar.gz (gzip archive), .tar.bz2 (bzip2 archive file), and .tar.xz (xz archive file).
- Creating a tar archive
To create a tar archive, use the “c” option followed by the “f” option to specify the name of the archive file. For example, to create an archive file named “example.tar” from the current directory and its contents, use the following command:
tar -cvf archive.tar /var/www
- Creating a tar.gz archive
To compress a tar archive, you can use the “z” option to compress the archive using gzip or the “j” option to compress the archive using bzip2. For example, to create a gzip compressed archive file named “example.tar.gz” from the current directory and its contents, use the following command:
tar -czf archive.tar.gz /var/www
- Creating a tar.bz2 archive
Compress all content of /var/www directory to a archive.tar.bz2 file including all subdirectories. This takes more time to compress than others and provides the highest compressed file that above.
tar -cjf archive.tar.gz /var/www
- Creating a tar.xz archive
Compress all content of /var/www directory to a archive.tar.xz file including all subdirectories. This takes more time to compress than others and provides the highest compressed file that above.
tar -cJf archive.tar.xz /var/www
How to Extract a tar archive
To extract files from a tar archive, use the “-x
” option followed by the “-f
” option to specify the name of the archive file. For example, to extract the contents of “archive.tar”, use the following command:
tar -xvf archive.tar
Similarly, you can extract content from the other tar formats like:
tar -xzf archive.tar.gz
tar -xjf archive.tar.bz2
tar -xJf archive.tar.xz
How to List content of a tar archive
To list the files in a tar archive, you can use the -t
(or --list
) option in the tar command. For example, to list the files in an archive called “archive.tar”, you can use the following command:
tar -tvf archive.tar
Similarly, you can list content of other tar formats like:
tar ztvf archive.tar.gz
tar jtvf archive.tar.bz2
tar Jtvf archive.tar.xz
How to Update a tar archive
To update an existing tar archive, you can use the -u
(or --update
) option in the tar command. For example, to update an archive called “archive.tar” with a new file called “new_file.txt”, you can use the following command:
tar -uvf archive.tar new_file.txt
Similarly, you can update content of other tar formats archives, like:
tar -uzf archive.tar.gz /var/www
tar -ujf archive.tar.bz2 /var/www
tar -uJf archive.tar.xz /var/www
Other Useful tar command examples
Here is some more useful tar command examples for handling the archive files. All the example, are displays for the default tar archive files. You can also perform the same operation for other tar formats by defining the corresponding command line options. For example to perform these steps with .tar.gz file include -z
option. Similarly use the -j
for .tar.bz2 files, and -J
for the .tar.xz files.
- Extracting a single file
To extract a single file from a tar archive, you can use the
-x
(or--extract
) option and specify the file name you want to extract. For example, to extract a file called “file.txt” from an archive called “archive.tar”, you can use the following command:tar -xvf archive.tar file.txt
- Listing files in a tar archive
To list the files in a tar archive, you can use the
-t
(or--list
) option in the tar command. For example, to list the files in an archive called “archive.tar”, you can use the following command:tar -tvf archive.tar
- Appending a file to a tar archive
To append a file to an existing tar archive, you can use the
-r
(or--append
) option in the tar command. For example, to append a file called “new_file.txt” to an archive called “archive.tar”, you can use the following command:tar -rvf archive.tar new_file.txt
- Displaying file content of a tar archive
To display the contents of a file within a tar archive, you can use the
-O
(or--to-stdout
) option in the tar command, followed by the file name. For example, to display the contents of a file called “file.txt” within an archive called “archive.tar”, you can use the following command:tar -Oxf archive.tar file.txt
Conclusion
The tar command is a powerful file archiving utility in Linux. It allows you to combine multiple files and directories into a single archive file and extract files from an archive. This article provided a comprehensive guide for beginners on the basics of tar command in Linux. Understanding the tar command will greatly simplify your work with files in Linux.
For more details, visit: http://www.gnu.org/software/tar/manual/tar.html