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] # ll total 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
?
Example:
?
123 |
[[email protected] TAR] # tar -tf file.tar file1.txt file2.txt |
Extract all files from tar
?
Example
?
123456 |
[[email protected] TAR] # tar -xf file.tar [[email protected] TAR] # ll total 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:
?
Example:
?
123 |
[[email protected] TAR] # tar -tvf file.tar -rw-r--r-- root /root 2770 2014-02-07 22:37 file1.txt -rw-r--r-- root /root 887 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.txt
file3.txt
file1.txt
[[email protected] TAR]
# tar -df file.tar file1.txt file2.txt file4.txt
tar
: file4.txt: Not found
in archive
tar
: Exiting with failure status due to previous errors
----Verbosely----
[r[email protected] TAR]
# tar -dvf file.tar file1.txt file2.txt
file2.txt
file1.txt
[[email protected] TAR]
# tar -dvf file.tar file1.txt file2.txt file6.txt
file2.txt
file1.txt
tar
: file6.txt: Not found
in archive
tar
: 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.tar file2.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.tar file1.txt file2.txt file3.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.txt file3.txt file1.txt file1.txt file3.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.txt file2.txt [[email protected] TAR] # tar -uf file.tar file1.txt file3.txt file2.txt [[email protected] TAR] # tar -tf file.tar file1.txt file2.txt file3.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/TAR2 file1.txt file2.txt [[email protected] TAR] # cd - /root/TAR2 [[email protected] TAR2] # ll total 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]
# ll
total 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