A detailed description of the tar command under Linux

Source: Internet
Author: User
Tags posix

A detailed description of the tar command under LinuxTar 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:
Reference # Tar--version
Tar (GNU tar) 1.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:
Reference-T,--list
List archived File contents directory
-X,--extract,--get
Parsing files from an archive file
-C,--create
Create a new archive file
The above three parameters, cannot exist simultaneously, can use only one, namely T/X/C
-F file,--file=file
Specify a backup file, or device, such as a tape drive/dev/st0
-V,--verbose
Show the execution of a command
-Z,--compress,--uncompress
Using the Compress command to process backup files
-Z,--gzip,--gunzip,--ungzip
To process a backup file using the gzip command
-j, I.,--bzip
Using the BZIP2 command to process backup files
-Z and-J are all compressed after the backup file is packaged, and the action affects the other parameters, which are mentioned later
-C directory,--directory=directory
Enter the specified directory first, then release
Simple to use:
Package all files in the/etc directory as Etc.tar files
Tar-cvf/tmp/etc.tar/etc

Package all files in the/etc directory and use gzip compression to etc.tar.gz files
Tar-zcvf/tmp/etc.tar.gz/etc

Package all files in the/etc directory and use bzip2 to compress to etc.tar.bz2 files
Here, tar.gz or tar.bz2 are customary, for convenience of identification, for example: tgz, etc., is not a specific condition, but recommended to follow the Convention
Tar-jcvf/tmp/etc.tar.bz2/etc

To view the contents of the backup package, add the-Z or-j parameter if the file is already compressed
Tar-ztvf/tmp/etc.tar.gz

By default, Tar is packaged with a relative path, so when you release it, you need to go to the directory directory and then unzip
Similarly, depending on the compression method used, the-Z or-j parameters should be added
Cd/tmp && tar-zxvf/tmp/etc.tar.gz

This is another way of writing, the result is the same as above, it will first enter the/tmp directory, and then release the package
TAR-ZXVF/TMP/ETC.TAR.GZ-C/tmp

Release a single etc/passwd file, as mentioned earlier, in the tar package is a relative path, so you cannot use "/etc/passwd"
Also, the-c parameter cannot be used here, and the release file is the relative path to the current path, with the directory release
Tar-zxvf/tmp/etc.tar.gz etc/passwd

Tar 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
Reference-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
Append only new files in the archive
-A,--catenate
After attaching a tar file to an archive file
--concatenate
Same as-a
--delete
Remove from archive (cannot be on tape!) )
-N Date,--newer=date,--after-date=date
Save only files that are newer than the specified date to the backup file
--exclude=pattern
Exclude files that match the lookup mode
-P,--absolute-names
The filename in the backup file uses the absolute path, not the "/" before the file name is removed, and the default is the relative path
-L,--one-file-system
The file system in which the file or directory is copied must be the same as the file system currently executed by the tar command, otherwise it will not be executed, that is, the files of the other partitions are not processed. (Problem with mount Mount Partition can be ignored)
--mode=permissions
When backing up, modify the file attributes in the join backup file to the specified properties, and the format and the chmod command accept the same format
--group=group
When backing up, set the group that belongs to the file in the backup file to the specified group
--owner=owner
When backing up, set the file owner into the backup file to the specified user
--numeric-owner
Replace user and group names with UID and GID
--recursion
Recursive mode (default)
--no-recursion
No recursive processing, that is, all files and folders under the specified directory are not backed up
--newer-mtime=date
Add only files whose contents has changed since date to the archive.
Only add files that have modified content after the specified date to the backup file
--anchored
When excluded, matches the entire file path in the exclusion match (default)
--no-anchored
Excludes paths that match the "/" in the matching formula when excluded
--ignore-case
Exclude case in exclude match when excluded
--no-ignore-case
Exclude case sensitivity in match-out (default)
--wildcards
Exclude matching wildcard characters (default)
--no-wildcards
Wildcard characters are not supported for exclude matches
--wildcards-match-slash
Exclude matching wildcard matches "/" (default)
--no-wildcards-match-slash
Exclude wildcard mismatch for match "/"
-x file,--exclude-from file
Specify a style file to exclude eligible files when the program executes
--remove-files
Delete files that have been added to the backup file
Example:
Create a full backup of the current directory to the Backup.tar file
(The reason for using find is that you can package all of the hidden files or files and directories that do not conform to the command specification)
Tar cvf-' find. -print' > Backup.tar

New files are not backed up until June 1, 2005
' 2005/06/01 ' -ZCVF home.tar.gz/home

Everything in/home and/etc directory is packaged in addition to the/home/dmtsai directory
Tar--exclude/home/dmtsai-zcvf myfile.tar.gz/home/*/etc

Delete etc/pbm2ppa.conf file from Etc.tar file
Tar--delete etc/pbm2ppa.conf-vf Etc.tar

Attach the Tmp.tar file to the back of Etc.tar
Tar-a TMP.TAR-VF Etc.tar

According to the style file list.txt after the-t parameter, the file specified in/etc is packaged, and LIST.TX is also the relative path by default.
" *.D " " *.conf " >-t LIST.TXT-CZVF etc.tar.gz/etc

Use a single command to implement the same functionality as in a pipeline
" *.D " " *.conf " | TAR-CZVF Etc.tar.gz-t-


※ It is worth noting several places
Reference 1)-F must follow the backup file, Xxx.tar or xxx.tar.gz
2) using--delete,-u,-r,-A, the back of the backup file must be uncompressed, if the backup file has been compressed, need to use Gunzip first decompression, after the operation and then gzip;
In other words, the tar.gz, tar.bz2 and other documents can not be directly manipulated;
3) When the backup is packaged, the default is the relative path, while the exclusion match defaults to full path matching
Therefore, when packaging, you will be prompted: "tar:removing leading '/' from member names"
Unless you specify with the-p parameter or a specific parameter 2. Release
Ref-K,--keep-old-files
When you restore a backup file, you do not overwrite files that already exist
-M,--modification-time,--touch
When you restore a file, the change time of the file is not changed
-P,--same-permissions
Resolve License permissions information, that is, retain the permissions information in the backup file
-S,--same-order,--preserve-order
Restore files in the same order as the backup files
--preserve
equals set-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
Do not use the owner information from the backup file and resolve the file in your own identity
--same-owner
Restore files using the same file owner
--overwrite
Overwrite files and directories that already exist
--overwrite-dir
Overwrite a directory that already exists
--recursive-unlink
Delete all files in the entire directory before releasing the connection
Example:
When you release a file, set it to the same owner in the backup file (for administrators only)
Tar--same-owner-xzvf etc.tar.gz
Through the pipeline, the/etc directory with tar entire "copy" to/backup/etc, without creating any temporary files
Because there is no need to generate a backup package file, the-Z parameter is not required here
Tar CVF-etc | Tar xvf--c/backup/etc
This is the same as the above principle, but only through SSH directly "copy" to the remote machine only
With SSH key matching, can be implemented without manual intervention of the backup work
Tar CVF-etc | (SSH [email protected] ' tar xvf--c/backup/etc ')

3. Operation of tape equipment
Some parameters apply only to tape devices, not to regular disk files.
Refer to-K file,--starting-file=file
Start restore from the specified file (in order)
-L Length,--tape-length=length
Set the capacity of the tape in 1024 bytes (bytes)
-M,--multivolume
Multi-volume mode (cannot operate on files) when setting up, restoring, or listing contents of a backup file
-V name,--label=name
Establish a backup file that uses the specified volume label
-W,--verify
Verify file is correct after writing to backup file
-F script,--info-script=script,--new-volume-script=script
The specified script file is executed when the tape is replaced and must be used in conjunction with the parameter-m
Example:
Package/bin,/usr/bin directories into ST0 tape devices
Tar Cvf/dev/st0/bin/usr/bin
Append the Old.dmp file to the St0 tape (using CVF, the original file will be deleted)
A simple way to use a tape drive under Linux can be found here:Click
Tar rvf/dev/st0 old.dmp

4. Other
There are some control parameters not discussed in detail here, please test yourself.
But a reminder, the-t parameter is more important OH.
Reference--null
-T reads the file name from the null device and overrides the-C setting
-O,--old-archive,--portability
Using the V7 format when writing data to a backup file
--posix
Use POSIX format when writing data to a backup file
-G,--incremental
Processing an incremental backup of the old GNU format
--rsh-command=command
Do not use the RSH command to connect to a remote host and use the specified command to
--suffix=suffix
Specifies the file to be backed up before the file is deleted, the suffix used by the backup file, and the default is "~"
--totals
When you create a backup file, list the size of the backup file that was established
Tar--totals-cvf Tmp.tar tmp/
-R,--block-number
When outputting information, block number information together
-S,--sparse
Save the file as a sparse file if there is a large number of consecutive 0 bytes in the file
-T file,--files-from=file
Specifies 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
Compress or decompress the backup file using the specified compression program
--volno-file=file
Use or update the volume numbers specified in the file files
-W,--interactive,--confirmation
When encountering a problem, ask the user to first confirm
-B,--read-full-records
Read data is reset sector size, only for BSD 4.2 pipe operation
-O,--to-stdout
Output files restored from a backup file to a standard output device
--version
Version information
--help
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.