HomeLinux21 Helpful Tar Command Examples for Each Linux Sysadmin

21 Helpful Tar Command Examples for Each Linux Sysadmin


The Linux “tar” stands for tape archive, which is utilized by a lot of Linux/Unix system directors to take care of tape drive backup in Linux.

The tar command in Linux is used to tear a group of recordsdata and directories right into a extremely compressed archive file generally known as tarball or tar, gzip and bzip in Linux.

The tar is essentially the most broadly used command to create compressed archive recordsdata that may be moved simply from one disk to a different disk or machine to machine.

tar - A Linux Archiving Utility
tar – A Linux Archiving Utility

On this article, we will probably be going to overview and talk about numerous tar command examples together with how one can create archive recordsdata utilizing (tar, tar.gz, and tar.bz2) compression, how one can extract archive recordsdata, extract a single file, view the content material of the file, confirm a file, add recordsdata or directories to the prevailing archive file, estimate the dimensions of tar archive file, and many others.

The primary objective of this information is to supply numerous tar command examples that may be useful so that you can perceive and change into an skilled in tar archive manipulation.

1. Create a tar File in Linux

The beneath instance of the tar command will create a tar archive file tecmint-14-09-12.tar for a listing /house/tecmint within the present working listing.

See the instance of the tar command in motion.

# tar -cvf tecmint-14-09-12.tar /house/tecmint/

/house/tecmint/
/house/tecmint/cleanfiles.sh
/house/tecmint/openvpn-2.1.4.tar.gz
/house/tecmint/tecmint-14-09-12.tar
/house/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
/house/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

Let’s talk about every possibility used within the above tar command to create a tar archive file.

  • c – Creates a brand new .tar archive file.
  • v – Verbosely present the .tar file progress.
  • f – File title sort of the archive file.

2. Create a tar.gz File in Linux

To create a compressed gzip archive file we use the choice z. For instance, the beneath command will create a compressed MyImages-14-09-12.tar.gz file for the listing /house/MyImages. (Word: tar.gz and tgz each are related).

# tar cvzf MyImages-14-09-12.tar.gz /house/MyImages
OR
# tar cvzf MyImages-14-09-12.tgz /house/MyImages

/house/MyImages/
/house/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg
/house/MyImages/RobertKristenviolent101201.jpg
/house/MyImages/Justintimerlake101125.jpg
/house/MyImages/Mileyphoto101203.jpg
/house/MyImages/JenniferRobert101130.jpg
/house/MyImages/katrinabarbiedoll231110.jpg
/house/MyImages/the-japanese-wife-press-conference.jpg
/house/MyImages/ReesewitherspoonCIA101202.jpg
/house/MyImages/yanaguptabaresf231110.jpg

3. Create a tar.bz2 File in Linux

The bz2 function compresses and creates an archive file lower than the dimensions of the gzip. The bz2 compression takes extra time to compress and decompress recordsdata than gzip, which takes much less time.

To create a extremely compressed tar file we use the choice j. The next instance command will create a Phpfiles-org.tar.bz2 file for a listing /house/php. (Word: tar.bz2 and tbz is much like tb2).

# tar cvfj Phpfiles-org.tar.bz2 /house/php
OR
# tar cvfj Phpfiles-org.tar.tbz /house/php
OR 
# tar cvfj Phpfiles-org.tar.tb2 /house/php

/house/php/
/house/php/iframe_ew.php
/house/php/videos_all.php
/house/php/rss.php
/house/php/index.php
/house/php/vendor.php
/house/php/video_title.php
/house/php/report.php
/house/php/object.html
/house/php/video.php

4. Extract Tar File in Linux

To untar or extract a tar file, simply situation the next command utilizing possibility x (extract). For instance, the beneath command will untar the file public_html-14-09-12.tar within the current working listing.

If you wish to untar in a unique listing then use possibility -C (specified listing).

## Untar recordsdata in Present Listing ##
# tar -xvf public_html-14-09-12.tar

## Untar recordsdata in specified Listing ##
# tar -xvf public_html-14-09-12.tar -C /house/public_html/movies/

/house/public_html/movies/
/house/public_html/movies/views.php
/house/public_html/movies/index.php
/house/public_html/movies/logout.php
/house/public_html/movies/all_categories.php
/house/public_html/movies/feeds.xml

5. Extract tar.gz File in Linux

To uncompress the tar.gz archive file, simply run the next command. If we wish to untar in several directories, simply use possibility -C and the listing path, as proven within the above instance.

# tar -xvf thumbnails-14-09-12.tar.gz

/house/public_html/movies/thumbnails/
/house/public_html/movies/thumbnails/katdeepika231110.jpg
/house/public_html/movies/thumbnails/katrinabarbiedoll231110.jpg
/house/public_html/movies/thumbnails/onceuponatime101125.jpg
/house/public_html/movies/thumbnails/playbutton.png
/house/public_html/movies/thumbnails/ReesewitherspoonCIA101202.jpg
/house/public_html/movies/thumbnails/snagItNarration.jpg
/house/public_html/movies/thumbnails/Minissha-Lamba.jpg
/house/public_html/movies/thumbnails/Lindsaydance101201.jpg
/house/public_html/movies/thumbnails/Mileyphoto101203.jpg

6. Extract tar.bz2 File in Linux

To uncompress the extremely compressed tar.bz2 file, simply use the next command. The beneath instance command will untar all of the .flv recordsdata from the archive file.

# tar -xvf videos-14-09-12.tar.bz2

/house/public_html/movies/flv/katrinabarbiedoll231110.flv
/house/public_html/movies/flv/BrookmuellerCIA101125.flv
/house/public_html/movies/flv/dollybackinbb4101125.flv
/house/public_html/movies/flv/JenniferRobert101130.flv
/house/public_html/movies/flv/JustinAwardmovie101125.flv
/house/public_html/movies/flv/Lakme-Vogue-Week.flv
/house/public_html/movies/flv/Mileyphoto101203.flv
/house/public_html/movies/flv/Minissha-Lamba.flv

7. Listing Tar Recordsdata in Linux

To record the contents of the tar archive file, simply run the next command with possibility t (record content material). The beneath command will record the content material of the uploadprogress.tar file.

# tar -tvf uploadprogress.tar

-rw-r--r-- chregu/workers   2276 2011-08-15 18:51:10 package2.xml
-rw-r--r-- chregu/workers   7877 2011-08-15 18:51:10 uploadprogress/examples/
index.php
-rw-r--r-- chregu/workers   1685 2011-08-15 18:51:10 uploadprogress/examples/
server.php
-rw-r--r-- chregu/workers   1697 2011-08-15 18:51:10 uploadprogress/examples/
data.php
-rw-r--r-- chregu/workers    367 2011-08-15 18:51:10 uploadprogress/
config.m4
-rw-r--r-- chregu/workers    303 2011-08-15 18:51:10 uploadprogress/
config.w32
-rw-r--r-- chregu/workers   3563 2011-08-15 18:51:10 uploadprogress/
php_uploadprogress.h
-rw-r--r-- chregu/workers  15433 2011-08-15 18:51:10 uploadprogress/
uploadprogress.c
-rw-r--r-- chregu/workers   1433 2011-08-15 18:51:10 bundle.xml

8. Listing tar.gz Recordsdata in Linux

Use the next command to record the content material 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. Listing tar.bz2 Recordsdata in Linux

To record the content material of the tar.bz2 file, situation the next command.

# tar -tvf Phpfiles-org.tar.bz2

drwxr-xr-x root/root         0 2012-09-15 03:06:08 /house/php/
-rw-r--r-- root/root      1751 2012-09-15 03:06:08 /house/php/iframe_ew.php
-rw-r--r-- root/root     11220 2012-09-15 03:06:08 /house/php/videos_all.php
-rw-r--r-- root/root      2152 2012-09-15 03:06:08 /house/php/rss.php
-rw-r--r-- root/root      3021 2012-09-15 03:06:08 /house/php/index.php
-rw-r--r-- root/root      2554 2012-09-15 03:06:08 /house/php/vendor.php
-rw-r--r-- root/root       406 2012-09-15 03:06:08 /house/php/video_title.php
-rw-r--r-- root/root      4116 2012-09-15 03:06:08 /house/php/report.php
-rw-r--r-- root/root      1273 2012-09-15 03:06:08 /house/php/object.html

10. Extract a File from Tar in Linux

To extract a single file known as cleanfiles.sh from cleanfiles.sh.tar use the next 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 known as index.php from the file Phpfiles-org.tar.bz2 use the next possibility.

# tar -jxvf Phpfiles-org.tar.bz2 house/php/index.php
OR
# tar --extract --file=Phpfiles-org.tar.bz2 /house/php/index.php

/house/php/index.php

13. Extract A number of Tar Recordsdata in Linux

To extract or untar a number of recordsdata from the tar, tar.gz, and tar.bz2 archive recordsdata. For instance, the beneath command will extract “file 1” and “file 2” from the archive recordsdata.

# 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 Recordsdata utilizing Wildcard in Linux

To extract a bunch of recordsdata we use wildcard-based extracting. For instance, to extract a bunch of all recordsdata whose sample 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'

/house/php/iframe_ew.php
/house/php/videos_all.php
/house/php/rss.php
/house/php/index.php
/house/php/vendor.php
/house/php/video_title.php
/house/php/report.php
/house/php/video.php

15. Add Recordsdata or Directories to Tar in Linux

So as to add recordsdata or directories to the prevailing tar archive recordsdata we use the choice r (append). For instance, we add the file xyz.txt and listing php to the prevailing 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 house/tecmint/
-rw-r--r-- root/root  15740615 2012-09-15 02:23:42 house/tecmint/
cleanfiles.sh
-rw-r--r-- root/root    863726 2012-09-15 02:23:41 house/tecmint/
openvpn-2.1.4.tar.gz
-rw-r--r-- root/root  21063680 2012-09-15 02:24:21 house/tecmint/
tecmint-14-09-12.tar
-rw-r--r-- root/root   4437600 2012-09-15 02:23:41 house/tecmint/
phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
-rw-r--r-- root/root     12680 2012-09-15 02:23:41 house/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 Recordsdata or Directories to tar.gz and tar.bz2 Recordsdata

The tar command doesn’t have the choice so as to add recordsdata or directories to an current compressed tar.gz and tar.bz2 archive file. If we do attempt will get the next error.

# tar -rvf MyImages-14-09-12.tar.gz xyz.txt

# tar -rvf Phpfiles-org.tar.bz2 xyz.txt

tar: This doesn't appear like a tar archive
tar: Skipping to subsequent header
xyz.txt
tar: Error exit delayed from earlier errors

17. How To Confirm tar, tar.gz, and tar.bz2 Archive File

To confirm any tar or compressed archived file we use possibility W (confirm). To do that, simply use the next examples of instructions. (Word: You can not do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).

# tar tvfW tecmint-14-09-12.tar

tar: This doesn't appear like a tar archive
tar: Skipping to subsequent header
tar: Archive incorporates obsolescent base-64 headers
tar: VERIFY FAILURE: 30740 invalid headers detected
Confirm -rw-r--r-- root/root    863726 2012-09-15 02:23:41 
/house/tecmint/openvpn-2.1.4.tar.gz
Confirm -rw-r--r-- root/root  21063680 2012-09-15 02:24:21 
/house/tecmint/tecmint-14-09-12.tar
tar: /house/tecmint/tecmint-14-09-12.tar: Warning: Can not stat: 
No such file or listing
Confirm -rw-r--r-- root/root   4437600 2012-09-15 02:23:41 
house/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
tar: /house/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm: 
Warning: Can not stat: No such file or listing
Confirm -rw-r--r-- root/root     12680 2012-09-15 02:23:41 
house/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
tar: /house/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: 
Warning: Can not stat: No such file or listing
Confirm -rw-r--r-- root/root         0 2012-08-18 19:11:04 xyz.txt
Confirm drwxr-xr-x root/root         0 2012-09-15 03:06:08 php/

18. Examine Tar File Measurement in Linux

To examine the dimensions of any tar, tar.gz, and tar.bz2 archive file, use the next command. For instance, the beneath command will show the dimensions 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 Recordsdata and Directories in Tar File

To exclude sure recordsdata and directories whereas making a tar.gz file, you should utilize the next command with the --exclude an possibility that may exclude recordsdata and directories when creating the tar archive file as proven.

# tar --exclude="file1.txt" -zcvf backup.tar.gz /house/tecmint
# tar --exclude="/house/tecmint/uploads" -zcvf backup.tar.gz /house/tecmint

Within the above command, we excluded file ‘file1.txt‘ and ‘uploads‘ listing from the /house/tecmint folder.

To exclude recordsdata with particular file extensions (.txt) when making a tar archive file, use:

# tar --exclude="*.txt" -zcvf backup.tar.gz /house/tecmint

20. Take away File and Listing from Tar File

The next tar command will take away a file or listing from the already created tar file utilizing the --delete possibility as proven.

# tar --delete -f backup.tar.gz file1.txt
# tar --delete -f backup.tar.gz '/house/tecmint/uploads'

21. Extract File Extension in Tar File

The next tar command will solely extract recordsdata with the precise extension .png from the tar archive file utilizing the --wildcards possibility as proven.

# tar -xvf backup.tar.gz --wildcards '*.png'

22. Tar Command Utilization and Choices

  • -c – create an archive file.
  • -x – extract an archive file.
  • -v – present the progress of the archive file.
  • -f – filename of the archive file.
  • -t – viewing the content material of the archive file.
  • -u – archives and provides to an current archive file.
  • -j – filter the archive by way of bzip2.
  • -z – filter the archive by way of gzip.
  • -r – append or replace recordsdata or directories to the prevailing archive recordsdata.
  • -W – Confirm an archive file.
  • -A – concatenates the archive recordsdata.
  • --wildcards – Specify patterns within the UNIX tar command.
  • --exclude – excludes recordsdata and directories when creating the archive.
  • --delete – take away the file and listing from the archive.

That’s it for now, hope the above tar command examples are sufficient so that you can study, and for extra data please use the man tar command.

# man tar

If you’re seeking to cut up any massive tar archive file into a number of components or blocks, simply undergo this text:

If we’ve missed any examples please do share with us by way of the remark field and please don’t neglect to share this text with your folks. That is one of the best ways to say thanks…..

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments