The tar commands are the Linux or Unix-like operating system commands that are used to compress, gather, and archive files. In this post, we will be discussing the tar command in Linux. So, make sure to read and note them all to use them on your system.
The tar stands for tape archive files that create a single archive file from a large number of files for easiness of the user. You can easily know the files with their names that will be ending with “tar” and such files are called “tarballs”.
There are various commands in Linux to create tarballs such as .tar.gz or .tgz archive files. Only a little part of the command keeps changing while the else part remains the same. Since most of the Linux beginner users are not familiar with all the commands, here we will be giving various examples for Linux tar command tape archive files.
Contents
- 1 Tar Command In Linux
- 1.1 Create A Tar Archive
- 1.2 Create a tar.gz Archive
- 1.3 Create tar.bz2 Archive
- 1.4 Untar tar Archive
- 1.5 Untar tar.gz Archive
- 1.6 Untar tar.bz2 Archive
- 1.7 View List Content Of tar Archive
- 1.8 View List Content Of tar.gz Archive
- 1.9 View List Content Of tar.bz2 Archive
- 1.10 Untar A Single File From tar Archive
- 1.11 Untar A Single File From tar.gz Archive
- 1.12 Untar A Single File From tar.bz2 File
- 1.13 Untar Multiple Files From tar, tar.gz, and tar.bz2 Archive
- 1.14 Untar Group Of Files Using Wildcard
- 1.15 Add Directories To tar Archive
- 1.16 Add Directories To tar.gz and tar.bz2 Archive
- 1.17 Verify tar, tar.gz, and tar.bz2 Archive
- 1.18 Check The Size Of tar, tar.gz, and tar.bz2 Archive
- 2 How To Use Tar Command In Linux
Tar Command In Linux
The tar command in Linux is not only one command. There are various commands that can be used to compress so many files into an archive. Also, you can extract, modify, and maintain the tape archive files with the tar commands. Therefore, knowing the tar commands is a must.
Create A Tar Archive
To create a tar archive, use the command given as follows. The tar command in Linux will be
# 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
The options that we have used above are for the purpose as given below.
- c – Creates a new tar archive.
- v – This verbosely shows tar file progress.
- f – The file type of the archive file.
Create a tar.gz Archive
For creating an archive with the gzip files, we have to use the “z” option. The commands tar.gz and tgz are the same and can be used to compress the gzip files into an archive file.
# 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
Create tar.bz2 Archive
The commands tar.bz2, tar.bz, and tb2 do the same function. To compress files that are lesser in size when compared to gzip files, we use the tar command in Linux as follows. The tar.bz2 takes more time to archive files when compared to the tar.gz archive files. Therefore, to create a highly compressed tar.bz2 archive file, we use the “j” option here.
# 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
Untar tar Archive
To untar means to extract an archive file. To extract a directory from the archive, we use the “x” option. If you want to extract from a different directory, you can use the “-c” option. The below tar command in Linux will untar the specified file.
## 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
Untar tar.gz Archive
The command to uncompress the tar.gz archive files is as shown in the image. Use the “-c” option to extract from a different directory.
# 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
Untar tar.bz2 Archive
The highly compressed tar.bz2 files can be untarred with the following command. All the .flv files will be extracted 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
View List Content Of tar Archive
For the command for viewing all the list contents that are compressed into an archive file, you have to run the command given below. This command will get you the list of all uploadprogress.tar files.
# 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
View List Content Of tar.gz Archive
To view the list content of tar.gz archive file, run the command as given below. You will get all the compressed tar.gz archive files on the screen.
# 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 3981 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
View List Content Of tar.bz2 Archive
To view the list content of tar.bz2 archive file, give the command as follows.
# 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
Untar A Single File From tar Archive
To uncompress or extract a single file called cleanfiles.sh from the archive tar file 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
Untar A Single File From tar.gz Archive
To untar a single file from the tar.gz highly compressed archive files, use the following tar command in Linux.
# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml OR # tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml tecmintbackup.xml
Untar A Single File From tar.bz2 File
To extract a single from a tar.bz2 highly compressed archive file, run the following tar command in Linux.
# 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
Untar Multiple Files From tar, tar.gz, and tar.bz2 Archive
If you want to untar multiple files from more than one archive file, you can give the following command.
# tar -xvf tecmint-14-09-12.tar "file 1" "file 2" # tar -zxvf MyImages-14-09-12.tar.gz "file 1" "file 2" # tar -jxvf Phpfiles-org.tar.bz2 "file 1" "file 2
Untar Group Of Files Using Wildcard
For extracting a group of files from archive files, we have to use Wildcard. To extract a group of files with .php type from tar, tar.gz, or tar.bza2 archive files, use the Linux tar command as given below.
# 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
Add Directories To tar Archive
To add a new directory to the existing archive file, we use the path option as r(append). For example, here we are adding php and xyz.txt directories to the already 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
Add Directories To tar.gz and tar.bz2 Archive
There are no commands for adding directories to an already existing tar.gz or tar.bz2 highly compressed archive file. If you try adding a directory to either of them, you will get an 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
Verify tar, tar.gz, and tar.bz2 Archive
The option we use to verify is W(verify). To verify a tar archive file, use the tar command in Linux as given below. However, verifying tar.gz and tar.bz2 is not supported.
# 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/
Check The Size Of tar, tar.gz, and tar.bz2 Archive
You can check the size of tar, tar.gz, and tar.bz2 archive files with the following command. The size will be given in 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
How To Use Tar Command In Linux
To use the tar command in Linux, just follow the simple steps given here.
- Go to the terminal program on your Linux system.
- Now, run “tar -zcvf file. tar. gz/path/to/dir/ command to compress a directory into a file.
- To compress a single file into an archive, use the “tar -zcvf file. tar .gz/path/to/dir/ command.
- And to compress multiple directories into an archive, run the “tar -zcvf file. tar. gz dir1 dir2 dir3 command.
This was the simple procedure to use the tar command in the Linux system. The commands vary according to the file size and number of files. Use the tar command in Linux that we have given above using this procedure.
Conclusion
We hope you found this article helpful and got an idea about the tar command in Linux. You can use the tar commands to compress large files into an archive file. For more related articles, visit our website Techly Fire.