18 Useful Tar Command Examples for Every Linux Sysadmin
The Linux “tar” stands for tape archive, which is used by a large number of Linux/Unix system administrators to deal with tape drive backup in Linux.
The tar command in Linux is used to rip a collection of files and directories into a highly compressed archive file commonly called tarball or tar, gzip, and bzip in Linux.
The tar is the most widely used command to create compressed archive files that can be moved easily from one disk to another disk or machine to machine.
In this article, we will be going to review and discuss various tar command examples including how to create archive files using (tar, tar.gz, and tar.bz2) compression, how to extract archive files, extract a single file, view the content of the file, verify a file, add files or directories to the existing archive file, estimate the size of tar archive file, etc.
The main purpose of this guide is to provide various tar command examples that might be helpful for you to understand and become an expert in tar archive manipulation.
1. Creating a Tar Archive File
The below example of the tar command will create a tar archive file tecmint-17-11-2023.tar
for a directory /home/tecmint in the current working directory.
See the example of the tar command in action.
tar -cvf tecmint-17-11-2023.tar /home/tecmint/
Let’s discuss each option used in the above tar command.
c
– Creates a new .tar archive file.v
– Verbosely show the .tar file progress.f
– File name type of the archive file.
2. Creating a Tar Archive with Compression
To create a compressed archive file, we use the option 'z'
(compress the archive using gzip). For example, the command below will generate a compressed file named 'MyImages-17-11-2023.tar.gz'
for the directory ‘/home/MyImages‘. (Note: 'tar.gz'
and 'tgz'
are interchangeable terms).
tar cvzf MyImages-17-11-2023.tar.gz /home/tecmint/MyImages OR tar cvzf MyImages-17-11-2023.tgz /home/tecmint/MyImages
3. Creating a tar.bz2 File in Linux
The bz2 feature compresses and creates an archive file that is smaller in size compared to gzip. However, the bz2 compression method requires more time for both compression and decompression, whereas gzip is faster in both processes.
To create a highly compressed new tar archive named Phpfiles-org.tar.bz2 by bundling all files and subdirectories within the /home/php directory, use the -j
option, which instructs tar to utilize the bzip2 compression algorithm, resulting in a smaller file size for efficient storage and transfer.
Note: tar.bz2 and tbz are similar terms, both referring to tb2.
tar cvfj Phpfiles-org.tar.bz2 /home/tecmint/php OR tar cvfj Phpfiles-org.tar.tbz /home/tecmint/php OR tar cvfj Phpfiles-org.tar.tb2 /home/tecmint/php
4. Extracting a Tar Archive
To untar or extract a tar file, simply execute the following command using the 'x'
option (extract). For instance, the command below will untar the file named ‘tecmint-17-11-2023.tar‘ in the present working directory.
tar -xvf tecmint-17-11-2023.tar
If you want to untar in a different directory then use option -C
(specified directory).
tar -xvf tecmint-17-11-2023.tar -C /home/tecmint/
5. Extracting a Compressed tar.gz Archive
To extract the contents of a compressed tar archive file named “MyImages-17-11-2023.tar.gz“, use the following command.
tar -xvf MyImages-17-11-2023.tar.gz
If you would like to extract in a different directory, just use the option -C
, which will extract the files into the specified directory as shown.
tar -xvf MyImages-17-11-2023.tar.gz -C /home/tecmint/
6. Extracting a tar.bz2 Archive
To uncompress the highly compressed tar.bz2 file, simply use the following command, which will untar all the files from the archive file.
tar -xvf Phpfiles-org.tar.bz2
7. Listing Contents of tar Archive
To list or view the contents of the tar archive file, simply run the following command with the -t
option (list content), which will display a detailed list of files and directories contained within the ‘tecmint-17-11-2023.tar‘ archive.
tar -tvf tecmint-17-11-2023.tar
8. Viewing Contents of tar.gz Archive
The following command will display a detailed list of files and directories contained within the “MyImages-17-11-2023.tar.gz” archive.
tar -tvf MyImages-17-11-2023.tar.gz
9. Printing Contents of tar.bz2 Archive
The following command provides an overview of the contents within the “Phpfiles-org.tar.bz2” archive without extracting the files.
tar -tvf Phpfiles-org.tar.bz2
10. Extracting a Single File from an Archive
To extract a single file named wp-cron.php
from the archive Phpfiles-org.tar.bz2, use the following command. Make sure to provide the correct path to the file you wish to extract.
tar -xvf Phpfiles-org.tar.bz2 home/tecmint/php/wp-cron.php
11. Extracting Multiple Files from an Archive
To extract or untar multiple files from tar
, tar.gz
, and tar.bz2
archive files, use the following command, which will extract files from the specified archive files.
tar -xvf tecmint-17-11-2023.tar "file1" "file2" tar -zxvf MyImages-17-11-2023.tar.gz "file1" "file2" tar -jxvf Phpfiles-org.tar.bz2 "file1" "file2"
12. Extract a Group of Files Using Wildcard in Linux
To extract a group of files we use wildcard-based extracting. For example, to extract a group of all files whose pattern begins with .php
from a tar
, tar.gz
, and tar.bz2
archive files, use:
tar -xvf Phpfiles-org.tar --wildcards '*.php' tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php' tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'
13. Appending Files to an Existing Archive
To add files or directories to the existing tar
, tar.gz
, and tar.bz2
archive files, use the option -r
, which will add the files to an existing archive file.
tar -rvf tecmint-14-09-12.tar xyz.txt tar -rvf MyImages-14-09-12.tar.gz xyz.txt tar -rvf Phpfiles-org.tar.bz2 xyz.txt
14. Verifying a Tar Archive File
The following command will display a detailed list of files and directories contained within the specified archive file, allowing you to visually verify the archive’s contents. If the archive is corrupted or incomplete, this verification process may reveal errors during the listing.
tar -tvf Phpfiles-org.tar.bz2
15. Checking Tar Archive File Size
To check the size of any tar
, tar.gz
, and tar.bz2
archive file, use the following command, which will display the size of the archive file in Kilobytes (KB).
tar -czf - tecmint-14-09-12.tar xyz.txt | wc -c tar -czf - MyImages-14-09-12.tar.gz xyz.txt | wc -c tar -czf - Phpfiles-org.tar.bz2 xyz.txt | wc -c
16. Excluding Files When Creating a Tar Archive
To exclude certain files and directories while creating a tar archive file, you can use the following command with the --exclude
an option that will exclude files and directories when creating the tar archive file as shown.
tar --exclude='file1.txt' -zcvf backup.tar.gz /home/tecmint tar --exclude='/home/tecmint/uploads' -zcvf backup.tar.gz /home/tecmint
In the above command, we excluded file ‘file1.txt‘ and ‘uploads‘ directory from the /home/tecmint folder.
To exclude files with specific file extensions (.txt)
when creating a tar archive file, use:
tar --exclude='*.txt' -zcvf backup.tar.gz /home/tecmint
17. Removing Files From a Tar Archive
The following tar command will delete a file or directory from an already created tar file using the --delete
option, as shown.
tar --delete -f backup.tar.gz file1.txt tar --delete -f backup.tar.gz '/home/tecmint/uploads'
18. Extracing File Extension From a Tar Archive
The following tar command will only extract files with the specific extension .png
from the tar archive file using the --wildcards
option as shown.
tar -xvf backup.tar.gz --wildcards '*.png'
19. Tar Command Usage and Options
Understanding the following various options and usage patterns of the ‘tar‘ command is essential for efficient file archiving, compression, and extraction.
-c
– create an archive file.-x
– extract an archive file.-v
– show the progress of the archive file.-f
– filename of the archive file.-t
– viewing the content of the archive file.-u
– archives and adds to an existing archive file.-j
– filter the archive through bzip2.-z
– filter the archive through gzip.-r
– append or update files or directories to the existing archive files.-W
– Verify an archive file.-A
– concatenates the archive files.--wildcards
– Specify patterns in the UNIX tar command.--exclude
– excludes files and directories when creating the archive.--delete
– remove the file and directory from the archive.
That’s it for now, hope the above tar command examples are enough for you to learn, and for more information please use the man tar command.
# man tar
If you are looking to split any large tar archive file into multiple parts or blocks, just go through this article:
If we’ve missed any examples please do share with us via the comment box and please don’t forget to share this article with your friends. This is the best way to say thanks…..