Linux tar command usage detailed

Source: Internet
Author: User
Tags bz2 gz file posix file permissions

tar [-cxtzjvfppn] files and directories ....

Example:

Example one: Package all the files in the/etc directory into a/tmp/etc.tar

[Root@linux ~]# tar-cvf/tmp/etc.tar/etc <== only packaged, not compressed!
[Root@linux ~]# tar-zcvf/tmp/etc.tar.gz/etc <== packaged, gzip compressed
[Root@linux ~]# tar-jcvf/tmp/etc.tar.bz2/etc <== after packaging to bzip2 compression
# Note that the file name after the parameter F is taken by itself, and we are accustomed to using the. Tar as an identification.
# If you add the z parameter, the. tar.gz or. tgz to represent the gzip-compressed tar file ~
# If you add the J parameter, use the. tar.bz2 as the file name.
# The above instruction will display a warning message when it is executed:
# "tar:removing leading"/"from member names" is a special set of absolute paths.

Example two: What documents are available in the above/tmp/etc.tar.gz document?

[Root@linux ~]# tar-ztvf/tmp/etc.tar.gz
# Since we use gzip compression, we need to check the files in the tar file
# We're going to have to add the Z parameter! It's very important!

Example three: Unzip the/tmp/etc.tar.gz file under/USR/LOCAL/SRC

[Root@linux ~]# CD/USR/LOCAL/SRC
[Root@linux src]# tar-zxvf/tmp/etc.tar.gz
# in the preset case, we can release the compressed file anywhere! In this example,
# I first transform the working directory underneath the/USR/LOCAL/SRC and unlock the/tmp/etc.tar.gz,
# then untie the catalog will be in/USR/LOCAL/SRC/ETC! In addition, if you enter the/USR/LOCAL/SRC/ETC
# you will find that the file attributes in this directory may be different from the/etc/.

Example four: under/tmp, I just want to untie the etc/passwd in the/tmp/etc.tar.gz.


[Root@linux ~]# Cd/tmp
[Root@linux tmp]# tar-zxvf/tmp/etc.tar.gz etc/passwd
# I can check the file name in the Tarfile through TAR-ZTVF, if only one file
# you can make it through this way! Notice that! The root directory within the etc.tar.gz/was taken away!

Example five: Back up all the files in the/etc/and save their permissions!

[Root@linux ~]# Tar-zcvpf/tmp/etc.tar.gz/etc
# This property of-p is important, especially if you want to preserve the properties of the original file!

Example six: In/home, a new file is backed up in 2005/06/01

[Root@linux ~]# tar-n "2005/06/01"-ZCVF home.tar.gz/home

Example seven: I want to back up/home,/etc, but don't/home/dmtsai

[Root@linux ~]# TAR–EXCLUDE/HOME/DMTSAI-ZCVF myfile.tar.gz/home/*/etc

Example eight: The/etc/package will be unpacked directly to the bottom of/tmp, without producing a file!
[Root@linux ~]# Cd/tmp
[Root@linux tmp]# tar-cvf–/etc | TAR-XVF-
# This action is a bit like cp-r/etc/tmp ~ still has its use!
# The place to note is that the output file becomes – and the input file becomes – and there is a | exist ~
# This separately represents standard output, standard input and Pipeline command!

Example seven: Exclude packaging You can use the-exclude parameter


Cases:

Exclude specified file:

Exclude folder A in the test directory

#tar-ZCVF test.tar.gz--exclude=test/a test

Exclude Folder A and B from the test directory

#tar-ZCVF test.tar.gz--exclude=test/a--exclude=test/b test

Such as:

Tar Cvfz backup_1.tar.gz/opt/data-exclude=resource*

Resource-named directories and files are excluded when packaging/opt/data.


If you want to include a specific list of files without a section, you can put files that are not included in a file Ext.txt

And then

Tar cvfz backup_1.tar.gz-exclude=resource*-x ext.txt/opt/data

It's OK.

The contents of my Ext.txt file are as follows:


$ cat/tmp/ext.txt

/opt/data/devs/locks/db-logs.lock
/opt/data/devs/locks/db.lock
/opt/data/devs/hooks/post-revprop-change.tmpl
/opt/data/devs/hooks/pre-lock.tmpl
/opt/data/devs/hooks/post-unlock.tmpl
/opt/data/devs/db/transactions
/opt/data/devs/db/txn-current-lock
/opt/data/devs/db/write-lock
/opt/data/devs/db/format
/opt/data/devs/conf/passwd

This allows you to not include any of the above files or directories when you package.

Parameters
A or--catenate adds warm pieces to the existing backup file.
Number of-b< blocks > or--blocking-factor=< blocks > set the number of blocks per record, each chunk size is 12Bytes.
-B or--read-full-records to reset the chunk size when reading data.
-C or--create to create a new backup file.
-c< destination directory > or--directory=< destination > switch to the specified directory.
-D or--diff or--compare contrast the differences between files within the backup file and the file system.
-f< backup files > or--file=< backup files > Specify backup files.
-f<script file > or--info-script=<script file > executes the specified script file each time the tape is replaced.
-G or--listed-incremental handles a large number of backups in the GNU format.
G-or--incremental handles a large number of backups of the old GNU format.
-H or--dereference does not establish a symbolic connection, directly copying the original file that the connection points to.
-I or--ignore-zeros ignores the 0 byte blocks in the backup file, which is EOF.
When a-K or--keep-old-files unlocks a backup file, it does not overwrite the existing file.
-k< file > or--starting-file=< file > Start the restore from the specified file.
Files or directories that are copied by-L or--one-file-system must be in the same file system as the tar instruction executes, otherwise they will not be replicated.
-l< Media capacity > or-tape-length=< Media capacity > Settings Store the capacity of each body, measured in 1024 bytes.
When you restore a file-M or--modification-time, the file's change time does not change.
-M or--multi-volume uses a multiple volume mode when establishing, restoring, or listing the contents of a backup file.
-n< date format > or--newer=< date > save only files that are newer than the specified date to the backup file.
The V7 format is used for writing data to the backup file by-O or--old-archive or--portability.
-O or--stdout outputs the files restored from the backup file to the standard output device.
-P or--same-permissions restores the file with the original file permissions.
The-p or--absolute-names filename uses an absolute name and does not remove the "/" number before the file name.
-R or--append the new file to the end of the existing backup file.
-R or--block-number lists the block numbers for each information in the backup file.
The order of the-s or--same-order restore files is the same as the order in which they are stored in the backup file.
-S or--sparse if a file contains a large number of consecutive 0 bytes, save this file as a sparse file.
-T or--list lists the contents of the backup file.
The-t< template file > or--files-from=< template file > Specifies a template file that contains one or more template styles, allowing tar to untie or set up files that meet the set criteria.
-U or--update only replaces files that are newer than those in the backup file.
-U or--unlink-first the file's connection before undoing the compressed file restore file.
The-V or--verbose displays the instruction execution process.
-v< volume name > or--label=< volume name > Establish a backup file that uses the specified volume name.
-W or--interactive ask the user before encountering a problem.
The confirmation file is correct after writing to the backup file-W or--verify.
-X or--extract or--get restores files from the backup file.
The-x< template file > or--exclude-from=< template file > Specifies a template file that contains one or more template styles, allowing AR to exclude files that meet the set criteria.
-Z or--gzip or--ungzip processes the backup file through the gzip instruction.
The backup file is processed by the-Z or--compress or--uncompress through the compress instruction.
-< device number >< storage density > Set the peripheral device number for the backup and the density of the data stored.
--after-date=< Date Time > The effect of this parameter is the same as the specified "-n" parameter.
--atime-preserve does not change the access time of the file.
--backup=< backup > or--backup to remove files before removing them.
--checkpoint lists directory names when reading backup files.
--concatenate the effect of this parameter is the same as the specified "-A" argument.
--confirmation the effect of this parameter is the same as the specified "-w" parameter.
--delete deletes the specified file from the backup file.
--exclude=< template style > exclude the style of the asked home model.
--group=< Group name > sets the group to which the files in the device file belong to the specified group.
--help online Help.
--ignore-failed-read ignores data read errors and does not interrupt execution of programs.
The--new-volume-script=<script file > The effect of this parameter is the same as the specified "-F" argument.
--newer-mtime only saved files that have changed.
--no-recursion does not do recursive processing, that is, all files and subdirectories under the specified directory are not processed.
--null read the file name from the null device.
--numeric-owner replaces user and group names with user identification codes and group identifiers.
--owner=< User name > Set the owner of the file that is added to the backup file to the specified user.
--posix uses the POSIX format when writing data to a backup file.
--preserve the effect of this parameter is the same as the specified "-ps" parameter.
--preserve-order the effect of this parameter is the same as the specified "-A" argument.
--preserve-permissions the effect of this parameter is the same as the specified "-P" argument.
The number of--record-size=< blocks > The effect of this parameter is the same as the specified "-B" parameter.
--recursive-unlink to undo the compressed file restore directory, first remove all files in the entire directory connection.
After the--remove-files file is added to the backup file, it is deleted.
--rsh-command=< executes instructions > sets instructions to be executed on the remote host to replace the rsh instruction.
--same-owner tries to restore your home with the same file owner.
--suffix=< backup Word Tail string > Remove files before backing up.
The file size is listed after the--totals backup file is established.
--use-compress-program=< executes the instructions > processes the backup files through the specified instructions.
--version Displays version information.
--volno-file=< Number file > Use the number in the specified file to replace the preset volume number.

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.