linux--Backup and restore--tar command

Source: Internet
Author: User
Tags bz2

File compression and decompression


Tar command

The TAR command creates archives for Linux files and directories.

With tar, you can create files (backup files) for a specific file, or you can change files in the file or add new files to the file. Tar was originally used to create archives on tape, and now users can create files on any device.

With the tar command, you can package a whole bunch of files and directories into a single file, which is useful for backing up files or combining several files into one file for easy 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 large file, compression is a large 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).





Grammatical structure

Tar (option) (parameter (package file required for new file))

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 setting;-K: Keep 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 filename uses an absolute name, does not remove the "/" number before the file name;-n< date format > or-- newer=< date time;: Save only files updated with the specified date to the backup file;--exclude=< template style;: Exclude files that conform to the template style.


Parameters

File or directory: Specify a list of files or directories to package

Example: backup

package all the files into Tar Package

TAR-CVF Log.tar Log2017.log #仅打包, not compressed. TAR-ZCVF log.tar.gz log2017.log #打包后, gzip compressed TAR-JCVF log.tar.bz2 log2017.log #打包后 to bzip2 compression

The file name after option F is taken by yourself, and we are accustomed to using. Tar as an identification. 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.


Check out the files in the tar package above

TAR-TVF Log.tar #查看tar包内的文件, two types of compressed packages can be viewed. TAR-ZTVF log.tar.gz #查看tar包内的文件, a package in gzip compressed format. TAR-JTVF log.tar.bz2 #查看tar包内的文件 to the package in BZIP2 compressed format.

File back up, and save its permissions:

TAR-ZCVPF log31.tar.gz log2014.log Log2015.log log2016.log

The-P property is important, especially if you want to preserve the properties of the original file.


In a folder, a new file is backed up than a date:

Tar-n "2012/11/13"-ZCVF log17.tar.gz test


The backup folder contents are excluded from some files:

Tar--exclude scf/service-zcvf scf.tar.gz scf/*



Example: Unzip

Unzip the TAR package:

Tar-zxvf/opt/soft/test/log.tar.gz

In the case of a preset, we can unzip the file anywhere


Extract only some of the files in tar:

Tar-zxvf/opt/soft/test/log30.tar.gz Log2013.log

I can check the file name of the tar package through TAR-ZTVF, if only a single file, you can unzip some files in this way!


Summarize:

In fact, the simplest way to use tar is simply to memorize the following:

Compression: tar-jcv-f filename.tar.bz2 file or directory name to be compressed query: Tar-jtv-f filename.tar.bz2 extract: Tar-jxv-f filename.tar.bz2-c directory to extract



This article is from the "Chuny" blog, make sure to keep this source http://chuny.blog.51cto.com/7755843/1909927

linux--Backup and restore--tar command

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.