Linux command (009)--Tar

Source: Internet
Author: User
Tags bz2

The TAR command creates archives (backups) of files and directories for Linux. With this command, you can create a backup for a specific file, or you can change the file or add a new file to it, and you can package a whole bunch of files and directories into a single file, which is useful for backing up files or merging several files into a file for network transmission.

The first thing to understand is two concepts: packaging and compression. Packaging refers to a large number of files or directories into a total file, compression is a compressed file through some compression algorithm into a small file. Why should we differentiate between these two concepts? This is due to the many compression programs in Linux that can only be compressed for one file, so that when you want to compress a lot of files, you have to first make a package (Tar command) of the whole bunch of files, and then compress the program (gzip bzip2 command).

First, the command syntax
Tar (options) (parameters)
Ii. Description of options
-A or--catenate: Add files to the existing backup file;-B: Set the chunk size;-C or--create: Create a new backup file;-c < directory;: This option is used for decompression, to extract in a specific directory, you can use this option. -D: Record file differences;-X or--extract or--get: Restore files from backup files;-T or--list: Lists the contents of the backup file;-Z or--gzip or--ungzip: processing the backup file via the gzip command; Z or--compress or--uncompress: The backup file is processed by compress instruction,-f< backup file > or--file=< backup file;: Specify backup file;-V or--verbose: show instruction execution process; R: Add files to the already compressed file;-U: Add changed and existing files to already existing compressed files;-j: Support bzip2 unzip file;-V: Show operation process;-L: File system boundary settings;-K: Leave the original file not overwritten;-M: Keep the file from being overwritten ;-W: Confirm the correctness of the compressed file;-P or--same-permissions: Restore the file with the original file permissions;-P or--absolute-names: The file name uses an absolute name, does not remove the "/" number before the filename;-n < date format > or--newer=< date time;: Save only files updated with the specified date to the backup file;--exclude=< template style;: Excludes files that conform to the template style.
Three, command parameters

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

Iv. examples

1. Package all files into a tar package

TAR-CVF 20161201.tar/backup/20161201      #仅打包, do not compress TAR-ZCVF 20161201.tar.gz/backup/20161201  #打包后, compress tar with gzip- JCVF 20161201.tar.bz2/backup/20161201 #打包后, compressed with bzip2

The file name after option F is taken by yourself, and we are accustomed to using. Tar. If the z option is added, a. tar.gz or. tgz is used to represent the gzip compressed tar package, or the. tar.bz2 as the TAR package name if the J option is added.

2. Check which files are included in the TAR package

TAR-ZTVF 20161201.tar.gz

Since gzip compression is used for log.tar.gz, it is necessary to add the z option when checking the files in the log.tar.gz package.

3. Unzip the TAR package

Tar-zxvf/u03/dsdb/20161201.tar.gz

4. Extract only some of the files in tar

Tar-zxvf/u03/dsdb/20161201.tar.gz cf_sp_c-478483010-20161201-01

5. In the folder, a new file is backed up than a date

Tar-n "2016/11/30"-ZCVF 20161201.tar.gz/backup/20161201

6. Exclude some files from the packaging process

Tar--exclude scf/service-zcvf scf.tar.gz scf/*
V. Problem-handling

Occurs when the tar command is executed: "tar:removing leading '/' from member names." First make it clear that this is not a fault, but a warning message, and the tar command converts the absolute path to a relative path.

The reason for the alarm is that, for a package file similar to TAR-ZCVF 20161201.tar.gz/backup/20161201, the unpacking will create "/backup/20161201" in the same directory as the tar file. This two level directory, for such a compressed package, the decompression method is to use the parameter "-C" refers to the extracted directory or use the "-P" parameter. Therefore, in a Linux system, when you use the TAR command to package files, it is generally not recommended to use absolute paths.

Linux command (009)--Tar

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.