[Reprint]15+ The use of a tar command, with an example

Source: Internet
Author: User
Tags compress archive

15+ the use of a tar command, with example http://www.oschina.net/question/12_146379

April 23 The development system practice >>>» of Wuhan OSC source--es6

The tar command is used to package many files into a single tape or disk archive, and to recover a list of files from an archive file. Useful when you need to send a large number of files by email or when transferring files. Here we introduce some basic usage scenarios.

The syntax for tar:

?
1 # tar [options] file.tar file1 file2 .. .. ..

File.tar is a tar archive, while other file1 and File2 are the files to be packaged.

For example we have two files File1.txt and File2.txt

?
1234 [[email protected] TAR]# lltotal 8-rw-r--r--. 1 root root 2770 Feb  7 22:37 file1.txt-rw-r--r--. 1 root root  887 Feb  7 22:38 file2.txt

Common usage scenarios for tar

Create a tar file
Grammar:

?
1 # tar -cf archive.tar files .. ..

Example:

?
123 [[email protected] TAR]# tar -cf file.tar file1.txt file2.txt[[email protected] TAR]# ll file.tar -rw-r--r--. 1 root root 10240 Feb  7 22:42 file.tar

List all files in the tar file

?
1 # tar -tf archive.tar

Example:

?
123 [[email protected] TAR]# tar -tf file.tar file1.txtfile2.txt

Extract all files from tar

?
1 tar-xf archive.tar

Example

?
123456 [[email protected] TAR]# tar -xf file.tar [[email protected] TAR]# lltotal 20-rw-r--r--. 1 root root  2770 Feb  7 22:37 file1.txt-rw-r--r--. 1 root root   887 Feb  7 22:38 file2.txt-rw-r--r--. 1 root root 10240 Feb  7 22:42 file.tar

Parameter options

1,-v,–verbose
verbosely List Files processed:
Syntax:
List all files in an Archive.tar verbosely:

?
1 tar-tvf archive.tar

Example:

?
123 [[email protected] TAR]# tar -tvf file.tar -rw-r--r-- root/root2770 2014-02-07 22:37 file1.txt-rw-r--r-- root/root887 2014-02-07 22:38 file2.txt

2,-c,–create
Create a new archive file

3,-t,–list
List the contents of the archive file

4,-x,–extract,–get
Extracting files from the archive

5,-d,–diff,–compare
Compare archiving and file system differences
Example:

?
12345678910111213141516 [[email protected] TAR]# tar -tf file.tar file2.txtfile3.txtfile1.txt[[email protected] TAR]# tar -df file.tar file1.txt file2.txt file4.txttar: file4.txt: Not found in archivetar: Exiting with failure status due to previous errors----Verbosely----[r[email protected] TAR]# tar -dvf file.tar file1.txt file2.txt file2.txtfile1.txt[[email protected] TAR]# tar -dvf file.tar file1.txt file2.txt file6.txtfile2.txtfile1.txttar: file6.txt: Not found in archivetar: Exiting with failure status due to previous errors

6,–delete
Delete a file from the archive
Example:
Remove File1.txt from archive File.tar

?
123 [[email protected] TAR]# tar --delete -f  file.tar  file1.txt [[email protected] TAR]# tar -tf file.tarfile2.txt

7,-r,–append
Append files to the archive
Example:
Append File3.txt to File.tar

?
12345 [[email protected] TAR]# tar -rf file.tar file3.txt[[email protected] TAR]# tar -tf file.tarfile1.txtfile2.txtfile3.txt

8,-a,–catenate,–concatenate
Append a tar archive to another archive file
Create a different tar file

?
1 [[email protected] TAR]# tar -cf archive.tar file1.txt file3.txt

Append method:

?
1234567 [[email protected] TAR]# tar -Af file.tar archive.tar[[email protected] TAR]# tar -tf file.tar file2.txtfile3.txtfile1.txtfile1.txtfile3.txt

9,–test-label
Test archive volume label and exit

Ten,-u,–update
Append only up-to-date files
Example:

?
12345678 [[email protected] TAR]# tar -tf file.tar file1.txtfile2.txt[[email protected] TAR]# tar -uf file.tar file1.txt file3.txt file2.txt[[email protected] TAR]# tar -tf file.tar file1.txtfile2.txtfile3.txt

One by one,-c,–directory=dir
Change Directory to DIR

For example:
Extract files to a different directory

?
123456789 [[email protected] TAR]# tar -xvf file.tar -C /root/TAR2file1.txtfile2.txt[[email protected] TAR]# cd -/root/TAR2[[email protected] TAR2]# lltotal 28-rw-r--r--. 1 root root 23250 Feb  7 23:11 file1.txt-rw-r--r--. 1 root root   887 Feb  7 22:38 file2.txt

-p,–preserve-permissions,
Retain the original file permissions when extracting files

Compress archive files, use BZIP and GZIP two ways

Parameters related to compression

-J,–BZIP2,
Compressing an archive using bzip2

Example:

?
1234567 [[email protected] TAR] # tar-jcf file.tar.bz file2.txt file1.txt [[email protected] TAR] # ll total -rw-r--r--. 1 root root 23250 feb  7 23:11 file1.txt -rw-r--r--. 1 root root   887 feb  7 22:38 file2.txt -rw-r--r--. 1 root root 30720 feb  7 23:30 file tar -rw-r--r--. 1 root root  1797 feb  7 23:42 file tar .bz

See, the file size above is reduced by BZIP to 1797 bytes.

-z,–gzip,
Archive with gzip compression

Example:

?
12345678 [[email protected] TAR]# tar -zcf file.tar.gz file2.txt file1.txt[[email protected] TAR]# lltotal 132-rw-r--r--. 1 root root 23250 Feb  7 23:11 file1.txt-rw-r--r--. 1 root root   887 Feb  7 22:38 file2.txt-rw-r--r--. 1 root root 30720 Feb  7 23:30 file.tar-rw-r--r--. 1 root root  1797 Feb  7 23:42 file.tar.bz-rw-r--r--. 1 root root  1673 Feb  7 23:45 file.tar.gz
That's it!

[Reprint]15+ The use of a tar command, with an example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.