Linux basic commands -tar

tar command can create archives for linux files and directories. The use of tar, you can create an archive (backup file) for a particular file, you can also change the file in the archive, or add new files to the archive. tar was originally used to create the file on tape, now, users can create files on any device. Using tar command, it can be a lot of files and directories, all packaged into a single file, which for the backup file or combination of files into a file for transmission to the network is very useful.

We must first understand two concepts: packing and compression. Packaging refers to a lot of files or directories into a general file; a large file compression sucked into a small file with some compression algorithms.

Why should we distinguish between these two concepts do? This stems from a lot of compression program can compress a file for Linux, so when you want to compress a lot of files, you have to first that a lot of files labeled as a package first (tar command), then use compression program to compress ( gzip  bzip2 command).

grammar

tar (option) (parameters)
Options
-A or - catenate: add a file to the backup file exists;
 - B: set the block size;
 -C or - Create: to create a new backup file;
 -C <directory> : This option is used in decompressing , to compress the solution in a specific directory, you can use this option.
- D: the difference in the log file;
 the -X---extract or or - GET: restore files from the backup file;
 -t or - List: lists the contents of the backup file;
 the -Z or --gzip or - ungzip : gzip command processed by the backup file;
 the -Z or --compress or - the uncompress: to process backup files compress instruction;
 -f <backup file> or - file = <backup file> : The backup file;
 -v or - - verbose: display instruction execution;
 - R & lt: add files to already compressed files;
 - U: added to change the existing file and the compressed file to already existing;
 - J: support bzip2 extracting file;
 - V: display operation process;
 - L: settings file system boundaries;
 -k: retain the existing file does not cover;
 - m: Reserved file is not covered;
 - W : confirm the correctness of the compressed file;
 -p or --same- Permissions: Restore the original file with the file permissions;
 -P or - absolute-names: file names use absolute name is not removed before the file name "/ " no;
 -N <date format> or --newer = <date time> : only save the updated file than to backup file specified date Lane;
 --exclude = <template style>: exclude files that match the template pattern.

parameter

File or directory: Specifies the list of files or directories to be packaged.

Examples

The papers are all packaged into a tar package :

tar -cvf log. tar log2012.log packaging only, not compressed! 
the tar -zcvf log. the tar after .gz log2012.log packaged to gzip compress 
 the tar -jcvf log. the tar after .bz2 log2012.log packaged to bzip2 compression

In the options ffile after the file name is taken of their own, we are accustomed .tar are used as identification. If you add zoptions, places .tar.gz or .tgz to represent a gzip-compressed tar archive; if the increase joption, as is .tar.bz2 tar package name.

Check out which files the above tar package :

tar ztvf log. tar gz
Because when we use gzip compression log.tar.gz, so to log.tar.gz access to documents in the package, you have to add zthis option a.

Unzip the tar package :

tar -zxvf /opt/soft/test/log.tar.gz
In the case of default, we can unlock the archive anywhere

The only part of the file in the extract from tar :

tar -zxvf /opt/soft/test/log30.tar.gz log2013.log
I can through tar -ztvfaccess to the file names in the tar package to, if just a single file, you can extract the part of the file through this way!

Files backed up, and save them permission :

tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log
This -pproperty is very important, especially when you want to keep the properties of the original file.

In the folder which, than the new backup file before a certain date :

tar -N "2012/11/13" -zcvf log17.tar.gz test
Backup folder contents are excluded section of the file:
tar --exclude scf/service -zcvf scf.tar.gz scf/*
In fact, the easiest to use tar as long as you can remember under way:
Compression: tar . -Jcv -f filename tar .bz2 to be compressed file or directory name 
query: tar . -Jtv -f filename tar .bz2 
decompression: tar . -Jxv -f filename tar .bz2 -C want to decompress the catalog

 

Guess you like

Origin www.cnblogs.com/lj7xun/p/10983679.html