Through SSH access to the server, it will inevitably use compression, decompression, packaging, unpacking, etc., this time the tar command is a necessary powerful tool. The most popular tar in Linux is the perfectly formed.
The tar command creates a file for Linux files and directories, and with tar, you can create files for a specific file, change files in the file, or add new files to the file. Tar was originally used to create files on tape, and now users can scatter the girl on any device to wear a file. 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 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).
The longest packaging program under Linux is tar, and the packages we use with the TAR program are often referred to as tar packages, and the tar package file commands usually end in. Tar. After the tar package is generated, you can use other programs to compress it.
1. Command format:
tar [Required parameters] [select parameters] [file]
2. Command function:
Used to compress and decompress files. The tar itself does not have compression capabilities. It is implemented by invoking the compression function
3. Command parameters:
The necessary parameters are as follows:
-A add compressed file to existing compression
-B Set Block file
-C Create a new compressed file
-D Record File differences
-R Add a file to a file that has been compressed
-u add changed and existing files to a compressed file that already exists
-X extracting files from compressed files
-T displays the contents of the compressed file
-Z Support gzip unzip files
-j Support bzip2 Extract files
-Z Support Compress unzip files
-V Show Operation procedure
-L File System boundary settings
-K Keep the original file without overwriting
-M includes files that are not overwritten
-W confirm the correctness of the compressed file
The optional parameters are as follows:
-B Set Number of blocks
-c switch to the specified directory
-f Specifies the compressed file
--HELP Display Help information
--version displaying version information
4. Frequently-used pressure/compression commands
Tar
Unpacking: Tar xvf 001.tar
Package: Tar CVF 001.tar 001 (Pack 001 files, note: not compressed.) )
. gz
Decompression 1:gunzip 001.gz
Decompression 2:gzip-d 001.gz
Compression: gzip 001
. tar.gz and. tgz
Decompression: Tar zxvf 001.tar.gz
Compression: Tar ZCVF 001.tar.gz 001
. bz2
Decompression 1:bzip2-d 001.bz2
Decompression 2:BUNZIP2 001.bz2
Compression: Bzip2-z 001
. tar.bz2
Decompression: Tar jxvf 001.tar.bz2
Compression: Tar JCVF 001.tar.bz2 001---command tar + parameters + new file name + files to package
. Z
Decompression: uncompress 001.Z
Compression: Compress 001
. Tar. Z
Unzip: Tar zxvf 001.tar. Z
Compression: Tar zcvf 001.tar. Z 001
. zip
Decompression: Unzip 001.zip
Compression: Zip 001.zip 001
. rar
Decompression: rar x 001.rar
Compression: rar a 001.rar 001
5. Usage examples
Example 1: Package all files into a tar package
Command:
TAR-CVF Log.tar 001.log
TAR-ZCVF log.tar.gz 001.log
TAR-JCVF log.tar.bz2 001.log
Description
TAR-CVF Log.tar 001.log Package only, do not compress
TAR-ZCVF log.tar.gz 001.log After packing, compress with gzip
TAR-ZCVF log.tar.bz2 001.log packed, compressed with bzip2
The file name after the parameter F is the one you went to, and we used to use the. Tar as an identification. If the z parameter is added, a. tar.gz or. tgz represents the gzip compressed tar package;
If the J parameter is added, the. tar.bz2 is used as the TAR package name.
Example 2: Check which files are in the tar package above
Command:
TAR-ZTVF log.tar.gz As we use the files in the gzip compressed log.tar.gz package, we have to add the Z parameter.
Example 3: Unzip the TAR package
Command: tar-zxvf/var/log.tar.gz
In the case of a preset, we can unzip the compressed file anywhere. (That is, the command that you execute under which path, it will unzip your compressed package to that path by default)
Example 4: Extract only some of the files in the/tar
Command:
Tar-zxvf/opt/soft/test.log.tar.gz 001.log
I can use TAR-ZXVF to check the file name in the tar package, if there is only one file, you can unzip some files in this way. (That is, if you know what is in the compression package, you can specify only to extract it out, indicating that this function is good chicken, I have not yet encountered that big to not be willing to all the extracted documents)
Example 5: Files are backed up and their permissions are saved
Command: TAR-ZCVPF log.tar.gz 001.log 002.log 003.log
Description: This-p parameter is used to specify the reserved property. --preserve
Example 6: In a folder, a new file is backed up than a date
Command: Tar-n "" 2016/06/30 "-ZCVF 001.tar.gz Test (This function can be used to write an automatic backup of the script, only update the files after a certain point in time)
Example 7: Exclude some files when backing up file contents
Command: Tar--exclude ACK/MONEY-ZCVF ack.tar.gz ack/* (Package ack under All files except money. In addition to the money, you have to pack away ^ ^)
Every day a Linux command (--tar) command