Introduction
The tar command is one of the most popular and widely used commands in Linux. It is used to create, extract, and manipulate archives. It is also used to transfer files between different systems. In this article, we will discuss 21 tar command examples in Linux. We will cover how to create, extract, and manipulate tar archives, as well as how to transfer files between systems. We will also discuss some of the most commonly used options and flags. By the end of this article, you will have a better understanding of how to use the tar command in Linux.
21 Tar Command Examples in Linux
1. Create an Archive File:
tar -cvf archive_name.tar /path/to/directory
2. Extract an Archive File:
tar -xvf archive_name.tar
3. Create a Compressed Archive File:
tar -zcvf archive_name.tar.gz /path/to/directory
4. Extract a Compressed Archive File:
tar -zxvf archive_name.tar.gz
5. Create a Bzip2 Compressed Archive File:
tar -jcvf archive_name.tar.bz2 /path/to/directory
6. Extract a Bzip2 Compressed Archive File:
tar -jxvf archive_name.tar.bz2
7. List the Contents of an Archive File:
tar -tvf archive_name.tar
8. Exclude Files When Creating an Archive File:
tar -cvf archive_name.tar –exclude=file1 –exclude=file2 /path/to/directory
9. Add Files to an Existing Archive File:
tar -rvf archive_name.tar file1 file2
10. Update Files in an Existing Archive File:
tar -uvf archive_name.tar file1 file2
11. Extract a Specific File from an Archive File:
tar -xvf archive_name.tar file1
12. Extract a Specific Directory from an Archive File:
tar -xvf archive_name.tar directory1
13. Create a Verbose Archive File:
tar -cvf archive_name.tar -v /path/to/directory
14. Create an Archive File Without Absolute Paths:
tar -cvf archive_name.tar –no-absolute-names /path/to/directory
15. Create an Archive File and Store at a Specific Location:
tar -cvf /path/to/archive_name.tar /path/to/directory
16. Create an Archive File and Store at a Specific Location with Compression:
tar -zcvf /path/to/archive_name.tar.gz /path/to/directory
17. Create an Archive File and Store at a Specific Location with Bzip2 Compression:
tar -jcvf /path/to/archive_name.tar.bz2 /path/to/directory
18. Extract an Archive File and Store at a Specific Location:
tar -xvf archive_name.tar -C /path/to/destination
19. Extract a Compressed Archive File and Store at a Specific Location:
tar -zxvf archive_name.tar.gz -C /path/to/destination
20. Extract a Bzip2 Compressed Archive File and Store at a Specific Location:
tar -jxvf archive_name.tar.bz2 -C /path/to/destination
21. Create a Tarball of Multiple Directories:
tar -cvf archive_name.tar dir1 dir2 dir3
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. Create a tar File in Linux
The below example of the tar command will create a tar archive file tecmint-14-09-12.tar for a directory /home/tecmint in the current working directory.
See the example of the tar command in action.
# tar -cvf tecmint-14-09-12.tar /home/tecmint/ /home/tecmint/ /home/tecmint/cleanfiles.sh /home/tecmint/openvpn-2.1.4.tar.gz /home/tecmint/tecmint-14-09-12.tar /home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm /home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
Let’s discuss each option used in the above tar command to create a tar archive file.
- c – Creates a new .tar archive file.
- v – Verbosely show the .tar file progress.
- f – File name type of the archive file.
2. Create a tar.gz File in Linux
To create a compressed gzip archive file we use the option z. For example, the below command will create a compressed MyImages-14-09-12.tar.gz file for the directory /home/MyImages. (Note: tar.gz and tgz both are similar).
# tar cvzf MyImages-14-09-12.tar.gz /home/MyImages OR # tar cvzf MyImages-14-09-12.tgz /home/MyImages /home/MyImages/ /home/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg /home/MyImages/RobertKristenviolent101201.jpg /home/MyImages/Justintimerlake101125.jpg /home/MyImages/Mileyphoto101203.jpg /home/MyImages/JenniferRobert101130.jpg /home/MyImages/katrinabarbiedoll231110.jpg /home/MyImages/the-japanese-wife-press-conference.jpg /home/MyImages/ReesewitherspoonCIA101202.jpg /home/MyImages/yanaguptabaresf231110.jpg
3. Create a tar.bz2 File in Linux
The bz2 feature compresses and creates an archive file less than the size of the gzip. The bz2 compression takes more time to compress and decompress files than gzip, which takes less time.
To create a highly compressed tar file we use the option j. The following example command will create a Phpfiles-org.tar.bz2 file for a directory /home/php. (Note: tar.bz2 and tbz is similar to tb2).
# tar cvfj Phpfiles-org.tar.bz2 /home/php OR # tar cvfj Phpfiles-org.tar.tbz /home/php OR # tar cvfj Phpfiles-org.tar.tb2 /home/php /home/php/ /home/php/iframe_ew.php /home/php/videos_all.php /home/php/rss.php /home/php/index.php /home/php/vendor.php /home/php/video_title.php /home/php/report.php /home/php/object.html /home/php/video.php
4. Extract Tar File in Linux
To untar or extract a tar file, just issue the following command using option x (extract). For example, the below command will untar the file public_html-14-09-12.tar in the present working directory.
If you want to untar in a different directory then use option -C (specified directory).
## Untar files in Current Directory ## # tar -xvf public_html-14-09-12.tar ## Untar files in specified Directory ## # tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/ /home/public_html/videos/ /home/public_html/videos/views.php /home/public_html/videos/index.php /home/public_html/videos/logout.php /home/public_html/videos/all_categories.php /home/public_html/videos/feeds.xml
5. Extract tar.gz File in Linux
To uncompress the tar.gz archive file, just run the following command. If we would like to untar in different directories, just use option -C and the directory path, as shown in the above example.
# tar -xvf thumbnails-14-09-12.tar.gz /home/public_html/videos/thumbnails/ /home/public_html/videos/thumbnails/katdeepika231110.jpg /home/public_html/videos/thumbnails/katrinabarbiedoll231110.jpg /home/public_html/videos/thumbnails/onceuponatime101125.jpg /home/public_html/videos/thumbnails/playbutton.png /home/public_html/videos/thumbnails/ReesewitherspoonCIA101202.jpg /home/public_html/videos/thumbnails/snagItNarration.jpg /home/public_html/videos/thumbnails/Minissha-Lamba.jpg /home/public_html/videos/thumbnails/Lindsaydance101201.jpg /home/public_html/videos/thumbnails/Mileyphoto101203.jpg
6. Extract tar.bz2 File in Linux
To uncompress the highly compressed tar.bz2 file, just use the following command. The below example command will untar all the .flv files from the archive file.
# tar -xvf videos-14-09-12.tar.bz2 /home/public_html/videos/flv/katrinabarbiedoll231110.flv /home/public_html/videos/flv/BrookmuellerCIA101125.flv /home/public_html/videos/flv/dollybackinbb4101125.flv /home/public_html/videos/flv/JenniferRobert101130.flv /home/public_html/videos/flv/JustinAwardmovie101125.flv /home/public_html/videos/flv/Lakme-Fashion-Week.flv /home/public_html/videos/flv/Mileyphoto101203.flv /home/public_html/videos/flv/Minissha-Lamba.flv
7. List Tar Files in Linux
To list the contents of the tar archive file, just run the following command with option t (list content). The below command will list the content of the uploadprogress.tar file.
# tar -tvf uploadprogress.tar -rw-r--r-- chregu/staff 2276 2011-08-15 18:51:10 package2.xml -rw-r--r-- chregu/staff 7877 2011-08-15 18:51:10 uploadprogress/examples/ index.php -rw-r--r-- chregu/staff 1685 2011-08-15 18:51:10 uploadprogress/examples/ server.php -rw-r--r-- chregu/staff 1697 2011-08-15 18:51:10 uploadprogress/examples/ info.php -rw-r--r-- chregu/staff 367 2011-08-15 18:51:10 uploadprogress/ config.m4 -rw-r--r-- chregu/staff 303 2011-08-15 18:51:10 uploadprogress/ config.w32 -rw-r--r-- chregu/staff 3563 2011-08-15 18:51:10 uploadprogress/ php_uploadprogress.h -rw-r--r-- chregu/staff 15433 2011-08-15 18:51:10 uploadprogress/ uploadprogress.c -rw-r--r-- chregu/staff 1433 2011-08-15 18:51:10 package.xml
8. List tar.gz Files in Linux
Use the following command to list the content of the tar.gz file.
# tar -tvf staging.tecmint.com.tar.gz -rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.tecmint.com-access_log -rw-r--r-- root/root 587 2012-08-29 18:35:12 staging.tecmint.com-access_log.1 -rw-r--r-- root/root 156 2012-01-21 07:17:56 staging.tecmint.com-access_log.2 -rw-r--r-- root/root 156 2011-12-21 11:30:56 staging.tecmint.com-access_log.3 -rw-r--r-- root/root 156 2011-11-20 17:28:24 staging.tecmint.com-access_log.4 -rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.tecmint.com-error_log -rw-r--r-- root/root 981 2012-08-29 18:35:12 staging.tecmint.com-error_log.1 -rw-r--r-- root/root 211 2012-01-21 07:17:56 staging.tecmint.com-error_log.2 -rw-r--r-- root/root 211 2011-12-21 11:30:56 staging.tecmint.com-error_log.3 -rw-r--r-- root/root 211 2011-11-20 17:28:24 staging.tecmint.com-error_log.4
9. List tar.bz2 Files in Linux
To list the content of the tar.bz2 file, issue the following command.
# tar -tvf Phpfiles-org.tar.bz2 drwxr-xr-x root/root 0 2012-09-15 03:06:08 /home/php/ -rw-r--r-- root/root 1751 2012-09-15 03:06:08 /home/php/iframe_ew.php -rw-r--r-- root/root 11220 2012-09-15 03:06:08 /home/php/videos_all.php -rw-r--r-- root/root 2152 2012-09-15 03:06:08 /home/php/rss.php -rw-r--r-- root/root 3021 2012-09-15 03:06:08 /home/php/index.php -rw-r--r-- root/root 2554 2012-09-15 03:06:08 /home/php/vendor.php -rw-r--r-- root/root 406 2012-09-15 03:06:08 /home/php/video_title.php -rw-r--r-- root/root 4116 2012-09-15 03:06:08 /home/php/report.php -rw-r--r-- root/root 1273 2012-09-15 03:06:08 /home/php/object.html
10. Extract a File from Tar in Linux
To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following command.
# tar -xvf cleanfiles.sh.tar cleanfiles.sh OR # tar --extract --file=cleanfiles.sh.tar cleanfiles.sh cleanfiles.sh
11. Extract a File from tar.gz in Linux
To extract a single file tecmintbackup.xml from the tecmintbackup.tar.gz archive file, use the command as follows.
# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml OR # tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml tecmintbackup.xml
12. Extract a File from tar.bz2 in Linux
To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.
# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php OR # tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php /home/php/index.php
13. Extract Multiple Tar Files in Linux
To extract or untar multiple files from the tar, tar.gz, and tar.bz2 archive files. For example, the below command will extract “file 1” and “file 2” from the archive files.
# tar -xvf tecmint-14-09-12.tar "file1" "file2" # tar -zxvf MyImages-14-09-12.tar.gz "file1" "file2" # tar -jxvf Phpfiles-org.tar.bz2 "file1" "file2"
14. 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 file.
# tar -xvf Phpfiles-org.tar --wildcards '*.php' # tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php' # tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php' /home/php/iframe_ew.php /home/php/videos_all.php /home/php/rss.php /home/php/index.php /home/php/vendor.php /home/php/video_title.php /home/php/report.php /home/php/video.php
15. Add Files or Directories to Tar in Linux
To add files or directories to the existing tar archive files we use the option r (append). For example, we add the file xyz.txt and directory php to the existing tecmint-14-09-12.tar archive file.
# tar -rvf tecmint-14-09-12.tar xyz.txt # tar -rvf tecmint-14-09-12.tar php drwxr-xr-x root/root 0 2012-09-15 02:24:21 home/tecmint/ -rw-r--r-- root/root 15740615 2012-09-15 02:23:42 home/tecmint/ cleanfiles.sh -rw-r--r-- root/root 863726 2012-09-15 02:23:41 home/tecmint/ openvpn-2.1.4.tar.gz -rw-r--r-- root/root 21063680 2012-09-15 02:24:21 home/tecmint/ tecmint-14-09-12.tar -rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/tecmint/ phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm -rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/tecmint/ rpmforge-release-0.5.2-2.el5.rf.i386.rpm -rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/ -rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php -rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php -rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php -rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php -rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php -rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php
16. Add Files or Directories to tar.gz and tar.bz2 Files
The tar command doesn’t have the option to add files or directories to an existing compressed tar.gz and tar.bz2 archive file. If we do try will get the following error.
# tar -rvf MyImages-14-09-12.tar.gz xyz.txt # tar -rvf Phpfiles-org.tar.bz2 xyz.txt tar: This does not look like a tar archive tar: Skipping to next header xyz.txt tar: Error exit delayed from previous errors
17. How To Verify tar, tar.gz, and tar.bz2 Archive File
To verify any tar or compressed archived file we use option W (verify). To do this, just use the following examples of commands. (Note: You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).
# tar tvfW tecmint-14-09-12.tar tar: This does not look like a tar archive tar: Skipping to next header tar: Archive contains obsolescent base-64 headers tar: VERIFY FAILURE: 30740 invalid headers detected Verify -rw-r--r-- root/root 863726 2012-09-15 02:23:41 /home/tecmint/openvpn-2.1.4.tar.gz Verify -rw-r--r-- root/root 21063680 2012-09-15 02:24:21 /home/tecmint/tecmint-14-09-12.tar tar: /home/tecmint/tecmint-14-09-12.tar: Warning: Cannot stat: No such file or directory Verify -rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm tar: /home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm: Warning: Cannot stat: No such file or directory Verify -rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm tar: /home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or directory Verify -rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt Verify drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/
18. Check Tar File Size in Linux
To check the size of any tar, tar.gz, and tar.bz2 archive file, use the following command. For example, the below command will display the size of the archive file in Kilobytes (KB).
# tar -czf - tecmint-14-09-12.tar | wc -c 12820480 # tar -czf - MyImages-14-09-12.tar.gz | wc -c 112640 # tar -czf - Phpfiles-org.tar.bz2 | wc -c 20480
19. Exclude Files and Directories in Tar File
To exclude certain files and directories while creating a tar.gz 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
20. Remove File and Directory from Tar File
The following tar command will remove a file or directory from the 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'
21. Extract File Extension in Tar File
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'
22. Tar Command Usage and Options
-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…..
21 Tar Command Examples in Linux
Tar is one of the most popular archiving tools in Linux. It is used to store and manipulate files in a compressed format. Tar is a very powerful tool that allows users to easily manage and manipulate files and directories. In this article, we will discuss the basics of the tar command and some of its most commonly used options.
1. Create a Tar File
To create a tar file, use the following command:
tar -cvf filename.tar directory_name
This command will create a tar file named filename.tar from the specified directory_name.
2. Extract a Tar File
To extract a tar file, use the following command:
tar -xvf filename.tar
This command will extract the contents of the tar file named filename.tar.
3. List Contents of a Tar File
To list the contents of a tar file, use the following command:
tar -tvf filename.tar
This command will list the contents of the tar file named filename.tar.
4. Add Files to an Existing Tar File
To add files to an existing tar file, use the following command:
tar -rvf filename.tar file_name
This command will add the specified file_name to the tar file named filename.tar.
5. Delete Files from an Existing Tar File
To delete files from an existing tar file, use the following command:
tar --delete -f filename.tar file_name
This command will delete the specified file_name from the tar file named filename.tar.
6. Compress a Tar File
To compress a tar file, use the following command:
tar -zcvf filename.tar.gz directory_name
This command will compress the specified directory_name into a tar file named filename.tar.gz.
7. Extract a Compressed Tar File
To extract a compressed tar file, use the following command:
tar -zxvf filename.tar.gz
This command will extract the contents of the compressed tar file named filename.tar.gz.
8. Create a Tar File with Absolute Paths
To create a tar file with absolute paths, use the following command:
tar -cvf filename.tar --absolute-names directory_name
This command will create a tar file named filename.tar from the specified directory_name with absolute paths.
9. Create a Tar File with Relative Paths
To create a tar file with relative paths, use the following command:
tar -cvf filename.tar --relative directory_name
This command will create a tar file named filename.tar from the specified directory_name with relative paths.
10. Create a Tar File with Exclusion List
To create a tar file with an exclusion list, use the following command:
tar -cvf filename.tar --exclude=pattern directory_name
This command will create a tar file named filename.tar from the specified directory_name excluding files and directories that match the specified pattern.
11. Create a Tar File with Multiple Directories
To create a tar file with multiple directories, use the following command:
tar -cvf filename.tar directory1 directory2 directory3
This command will create a tar file named filename.tar from the specified directories.
12. Create a Tar File with Multiple Files
To create a tar file with multiple files, use the following command:
tar -cvf filename.tar file1 file2 file3
This command will create a tar file named filename.tar from the specified files.
13. Create a Tar File with Verbose Output
To create a tar file with verbose output, use the following command:
tar -cvf filename.tar -v directory_name
This command will create a tar file named filename.tar from the specified directory_name with verbose output.
14. Create a Tar File with Gzip Compression
To create a tar file with gzip compression, use the following command:
tar -zcvf filename.tar.gz directory_name
This command will create a tar file named filename.tar.gz from the specified directory_name with gzip compression.
15. Create a Tar File with Bzip2 Compression
To create a tar file with bzip2 compression, use the following command:
tar -jcvf filename.tar.bz2 directory_name
This command will create a tar file named filename.tar.bz2 from the specified directory_name with bzip2 compression.
16. Create a Tar File with XZ Compression
To create a tar file with xz compression, use the following command:
tar -Jcvf filename.tar.xz directory_name
This command will create a tar file named filename.tar.xz from the specified directory_name with xz compression.
17. Create a Tar File with Lzip Compression
To create a tar file with lzip compression, use the following command:
tar --lzip -cvf filename.tar.lz directory_name
This command will create a tar file named filename.tar.lz from the specified directory_name with lzip compression.
18. Create a Tar File with Lzma Compression
To create a tar file with lzma compression, use the following command:
tar --lzma -cvf filename.tar.lzma directory_name
This command will create a tar file named filename.tar.lzma from the specified directory_name with lzma compression.
19. Create a Tar File with Lzop Compression
To create a tar file with lzop compression, use the following command:
tar --lzop -cvf filename.tar.lzo directory_name
This command will create a tar file named filename.tar.lzo from the specified directory_name with lzop compression.
20. Create a Tar File with Zstd Compression
To create a tar file with zstd compression, use the following command:
tar --zstd -cvf filename.tar.zst directory_name
This command will create a tar file named filename.tar.zst from the specified directory_name with zstd compression.
21. Create a Tar File with Multiple Compression Algorithms
To create a tar file with multiple compression algorithms, use the following command:
tar --use-compress-program=program1,program2,program3 -cvf filename.tar directory_name
This command will create a tar file named filename.tar from the specified directory_name with the specified compression algorithms.