A detailed description of the tar command under Linux

Source: Internet
Author: User
Tags gz file

Transferred from: http://blog.chinaunix.net/uid-20639775-id-154567.html

Tar is one of the most commonly used backup tools in a Linux environment. Tar (tap archive) is intended to manipulate tape files, but Linux-based file manipulation mechanisms can also be applied to normal disk files. Tar can be used to create, restore, view, manage files, or to easily append new files to the backup file, or to update only part of the backup file, as well as extract and delete the specified file. Familiar with its common parameters, can facilitate the daily system management work.

First, version
Or the same sentence, different operating systems, the tar command parameters are somewhat different:

Tar --versiontartar1.14

Second, simple operation

Command format:

Tar [Options] [Tarfile] [other-Files] Tar--long-option-function-options files

There are many parameters that can be used in tar, and some of the most commonly used parameters are listed first:

-T,--List List of archived file content directories-X,--extract,--get parse file from archive file-C,--create creates a new archive file above three parameters, cannot exist simultaneously, can only use one, namely T/x/C-Ffile, --file=fileSpecify a backup file, or device, such as a tape drive/dev/st0-V,--verbose show command execution process-Z,--compress,--uncompress using the Compress command to process backup files-Z,--gzip, --Gunzip, --ungzip using the gzip command to process backup files-j, I.,--bzip using the bzip2 command to process backup files-Z and-J is a compressed operation after the backup file is packaged, and the operation affects other parameters, which are mentioned later-C directory,--directory=Directory first into the specified directory, and then release the simple use:/all files in the ETC directory are packaged as Etc.tar filesTar-cvf/tmp/etc.Tar/etc toAll files in the/etc directory are packaged and compressed to etc using gzip.Tar. gz fileTar-zcvf/tmp/etc.Tar. GZ/etc toAll files in the/etc directory are packaged and compressed to etc using bzip2.Tar. bz2 file here,Tar. gz or tar.bz2 are customary, for convenience of identification, for example: tgz, etc., is not a specific condition, but it is recommended to follow the ConventionTar-jcvf/tmp/etc.Tar. bz2/etc View the contents of the backup package, if it is a compressed file, you need to add-Z OR-J ParameterTar-ztvf/tmp/etc.Tar. GZ By default, the tar is packaged using a relative path, so, when released, you need to go to the directory directory and then unzip the same, depending on the compression method used, you should add-Z OR-J-Parameter CD/tmp &&Tar-zxvf/tmp/etc.Tar. Gz this is another way of writing, and the result is the same as above, which first enters/tmp directory, and then release the packageTar-zxvf/tmp/etc.Tar. gz-c/TMP releases a single etcThe/passwd file, as mentioned earlier, is a relative path in the tar package, so you cannot use the "/etc/passwd"Moreover, it is not possible to use-c parameter, the release file is the relative path of the current path, and with the directory releaseTar-zxvf/tmp/etc.Tar. GZ etc/passwdTar also supports a number of additional parameters that allow us to control the backup and release. 

Three, more parameters

The three most common operations are described in order.
1. Backup
-D,--diff, --Compare find out the difference between an archive file and a file system-R,--append after attaching a file to an archive file-U,--update only appends new files in the archive-A,--Catenate willTarafter the file is attached to the archive file--Concatenate and-A Same--Delete is removed from the archive (cannot be on tape!) )-NDate,--newer=Date,--after-Date=Datesave only files that are newer than the specified date to the backup file--exclude=pattern excludes files that match the find pattern-P,--absolute-The file name in the names backup file uses an absolute path, without removing the file name before the "/", the default is relative path-L,--one-file-the file system in which system copies files or directories must be the same as the file system currently being executed by the tar command, otherwise it will not be executed, that is, the files of other partitions are not processed. (Problem with mount Mount Partition can be ignored)--mode=permissions The file attributes in the backup file are modified to the specified properties when the backup is taken, the format and the chmod command accept the same format--group=when group is backed up, sets the groups to which the files in the backup file belong to the specified group--owner=The owner of the file that was added to the backup file is set to the specified user when owner backs up--numeric-owner replaces user name and group name with UID and GID--recursion recursive mode (default)--no-recursion does not perform recursive processing, that is, all files and folders under the specified directory are not backed up--newer-mtime=DateAdd only files whose contents has changed sinceDateto the archive. Only add files that have been modified after the specified date to the backup file--anchored when excluded, matches the entire file path in the exclusion match (default)--no-anchored when excluded, exclude matches in match "/"After the path--ignore- CaseExclude case in exclude match when excluded--no-ignore- CaseExclude case sensitivity in match-out (default)--wildcards exclusion matching wildcard characters (default)--no-wildcards exclusion match does not support wildcard characters--wildcards-match-slash exclude matching wildcard matches "/"(default)--no-wildcards-match-Slash match wildcard mismatch "/"-Xfile,--exclude-fromfileSpecify a style file to exclude eligible files when the program executes--remove-files Delete An example of a file that has been added to a backup file: Create a full backup of the current directory to the Backup.tar file (the reason for using find is that you can package all implied files or files and directories that do not conform to the command specification)TarCVF-'Find. -print ' > Backup.Tarnew files are not backed up until June 1, 2005Tar-N'2005/06/01'-ZCVF home.Tar. GZ/Home exceptOutside the/home/dmtsai directory,/Home and/all things in the ETC directory are packagedTar--EXCLUDE/HOME/DMTSAI-ZCVF myfile.Tar. gz/home/*/ etc Delete etc/pbm2ppa.conf file in Etc.tar file tar--delete etc/pbm2ppa.conf-vf etc.tar append tmp.tar file to Etc.tar tar-a Tmp.tar -VF Etc.tar According to the style file of the-t parameter list.txt, packaging the file specified in/etc, similarly, list.tx default is also a relative path to find. -name "*.D"-o-name "*.conf" > List.txttar-t LIST.TXT-CZVF etc.tar.gz/etc through the pipeline, using a command to achieve the same function above find. -name "*.D"-o-name "*.conf" | TAR-CZVF Etc.tar.gz-t-

※ It is worth noting several places

1)-F must follow the backup file, Xxx.tar or XXX. Tar . GZ 2 gzip In other words, it is not possible to tar.gz, Tar . bz2 and other documents are directly operated; 3 When a backup is packaged, the default is a relative path, and the exclusion match defaults to full path matching, so when you package, you are prompted: " Tar: Removing leading '/' from member names unless you use-

2. Release

-K,--keep-old-files do not overwrite a file that already exists when you restore a backup file-M,--modification- Time, --TouchWhen you restore a file, the change time of the file is not changed-P,--same-permissions Resolve permission information, that is, retain the permissions in the backup file-S,--same-order,--preserve-Order restores files in the same order as the backup files--Preserve equals simultaneous setting-P and-s--no-same-permissions does not resolve license permissions information, which is the default setting for normal users and affects only the operations that affect administrators--no-same-owner does not use ownership information from the backup file and resolves the file in your own identity--same-owner restores files using the same file owner--overwrite overwrite files and directories that already exist--overwrite-diroverwrite a directory that already exists--recursive-unlinkbefore releasing, remove the connection example for all files in the entire directory: when releasing the file, set to the same owner in the backup file (for administrators only)Tar--SAME-OWNER-XZVF etc.Tar. GZ through the pipe, put/etc directory with tar entire "copy" to/backup/etc, without having to create any temporary files because there is no need to generate a backup package file, so there is no need to-Z parameterTarCVF-etc |TarXVF--c/backup/etc here with the above principle is the same, but by SSH directly "copy" to the remote machine only with SSH key matching, can achieve without manual intervention of the backup workTarCVF-etc | (SSH[Email protected]192.168.228.244 'Tar xvf--c/backup/etc')
3. Operation of tape equipment
some parameters apply only to tape devices, not to regular disk files. References-Kfile,--starting-file=filestart restore from the specified file (in order)-L Length,--tape-length=length set the capacity of the tape in 1024 bytes (bytes)-M,--multivolume in Multi-volume mode (cannot operate on files) when creating, restoring, or listing contents of a backup file-V name,--label=name establishes a backup file using the specified volume label-W,--Verify Check if file is normal after writing to backup file-F script,--Info-script=script,--new-volume-script=script is executed when the tape is replaced and must be specified with the parameter-m Use the same example:/bin,/usr/The Bin directory is packaged in a st0 tape deviceTarcvf/dev/st0/bin/usr/The bin adds the Old.dmp file to the St0 tape (using CVF, which will delete the original file) in a simple way to use a tape drive under Linux, as seen here: clickTarRvf/dev/st0 old.dmp
4. Other
There are some control parameters not discussed in detail here, please test yourself. But a reminder,-T parameter is more important OH. References--NULL-T reads the file name from the null device, overwriting-Settings for C-O,--old-archive,--portability using V7 format when writing data to a backup file--POSIX format used to write data to a backup file-G,--incremental processing An incremental backup of the old GNU format--rsh-command=The command does not use the RSH command to connect to the remote host and uses the specified commands to--suffix=suffix Specifies the file to be backed up before the file is deleted, and the suffix used by the backup file defaults to "~"--totals When you create a backup file, list the size of the backup file that was establishedTar--TOTALS-CVF tmp.TarTmp/-r,--block-Number in the output information, block number information together-S,--sparse If a file has a large number of contiguous 0 bytes, save the file as a sparse file-Tfile,--files-from=filespecifies a style file with a file content of one or more conditional styles for the program to restore or create files that meet specified criteria--use-compress-program=program compresses or extracts the backup files using the specified compressor--volno-file=file uses or updates the volume number specified in the file-W,--interactive,--confirmation when encountering a problem, ask the user to first confirm-B,--read-full-Records read data is reset sector size, only applicable to BSD4. 2 Pipe Operation-O,--to-stdout output files restored from backup files to standard output devices--Version Information--Help Information


A detailed description of the tar command under Linux

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.