Scenario: Compression and decompression commands are often used in Linux environments, so it is necessary to learn this command!
Original source: http://www.cnblogs.com/peida/archive/2012/11/30/2795656.html
1 Introduction
Through SSH access to the server, will inevitably use compression, decompression, packaging, unpacking, etc., this time the tar command is an essential tool for a powerful. The most popular tar in Linux is the perfectly formed, the most powerful.
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 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 most common packaging program under Linux is tar, which is often referred to as the TAR package, and the tar package file commands are usually terminated with. Tar. After the tar package is generated, you can use other programs to compress it.
1.1 Tar Command Introduction
1. Command format:
tar[necessary parameters [selection 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 Chunk size
-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 reserved files 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
1.2 Common Insights compression/compression commands
PS:XVF Unpacking, CVF packaging
Tar
Unpacking: Tar xvf Filename.tar
Package: Tar cvf filename.tar DirName
(Note: Tar is packaged, not compressed!) )
. gz
Decompression 1:gunzip filename.gz
Decompression 2:gzip-d filename.gz
Compression: gzip FileName
. tar.gz and. tgz
Decompression: Tar zxvf FileName.tar.gz
Compression: Tar zcvf FileName.tar.gz DirName
. bz2
Decompression 1:bzip2-d filename.bz2
Decompression 2:BUNZIP2 filename.bz2
Compression: Bzip2-z FileName
. tar.bz2
Decompression: Tar jxvf FileName.tar.bz2
Compression: Tar jcvf FileName.tar.bz2 DirName
. BZ
Decompression 1:bzip2-d filename.bz
Decompression 2:BUNZIP2 filename.bz
Compression: Unknown
. tar.bz
Decompression: Tar jxvf FileName.tar.bz
Compression: Unknown
. Z
Decompression: uncompress filename.z
Compression: Compress FileName
. Tar. Z
Decompression: Tar zxvf filename.tar.z
Compression: Tar zcvf filename.tar.z DirName
. zip
Decompression: Unzip Filename.zip
Compression: Zip Filename.zip DirName
. rar
Decompression: rar x Filename.rar
Compression: rar a Filename.rar DirName
1.3 Usage Examples
Example 1: Package all files into a tar package
Command:
TAR-CVF Log.tar Log2012.log
TAR-ZCVF log.tar.gz Log2012.log
TAR-JCVF log.tar.bz2 Log2012.log
Output:
[Email protected] test]#Ls-al Log2012.log---xrw-r--1 root root 302108 11-13 06:03Log2012.log[[email protected] test]# tar - cvf log.tar log2012.log log2012.log[[email protected] test]# tar - zcvf log.tar.gz log2012.loglog2012.log[[email protected] test]# tar - jcvf log.tar.bz2 log2012.log log2012.log[[email protected] test]# ls -al *.tar*-rw-r--r--1 root root 307200 11-29 17:54Log.tar-rw-r--r--1 root root 1413 11-29 17:55log.tar.bz2-rw-r--r--1 root root 1413 11-29 17:54 log.tar.gz
Description
Compress into different forms of compressed files
TAR-CVF Log.tar Log2012.log Package only, do not compress!
TAR-ZCVF log.tar.gz log2012.log After packing, compress with gzip
TAR-ZCVF log.tar.bz2 Log2012.log is packaged to bzip2 compression
The file name after the parameter F is taken by itself, and we are accustomed to using. Tar as an identification. If the z parameter 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 parameter is added.
Example 2: Check which files are in the tar package above
Command:
TAR-ZTVF log.tar.gz
Output:
TAR-ZTVF log.tar.gz---xrw-r--root/root 302108 2012-11-13 06:03:25 log2012.log
Description
Since we use gzip-compressed log.tar.gz, we need to add Z to this parameter when looking at the files in the log.tar.gz package.
Example 3: unzip the tar package
Command:
Tar-zxvf/opt/soft/test/log.tar.gz
Output:
ll tar-zxvf/opt/soft/test/ log.tar.gzlslog2012.log[[email protected] test3]#
Description
In the case of a preset, we can unzip the file anywhere.
We can arbitrarily in any directory, to unzip the other directory of compressed files
Example 4: Extract only some of the files in the/tar
Command:
Tar-zxvf/opt/soft/test/log30.tar.gz Log2013.log
Output:
[Email protected] test]#Tar- zcvf log30.tar.gz log2012.log log2013.log log2012.loglog2013.log[[email protected] test]# ls -al log30.tar.gz-rw-r--r--1 root root 1512 11-30 08:19Log30.tar.gz[[email protected] test]# tar - zxvf log30.tar.gz log2013.loglog2013.log[[email protected] test]# ll-rw-r--r--1 root root 1512 11-30 08:19Log30.tar.gz[[email protected] test]# CD Test3[[email protected] test3]# tar -zxvf/opt/soft/test/ log30.tar.gz log2013.loglog2013.log[[email protected] test3]# ll Total4-rw-r--r--1 root root 61 11-13 06:03Log2013.log[[email protected] test3]#
Description
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! (part decomposition pressure)
Example 5: Files are backed up and their permissions are saved
Command:
TAR-ZCVPF log31.tar.gz log2014.log Log2015.log log2016.log
Output:
[[Email protected]ost test]# ll Total0-rw-r--r--1 root root 0 11-13 06:03Log2014.log-rw-r--r--1 root root 0 11-13 06:06Log2015.log-rw-r--r--1 root root 0 11-16 14:41Log2016.log[[email protected] test]# tar - zcvpf log31.tar.gz log2014.log log2015.log log2016.log log2014.loglog2015.loglog2016.log[[email Protected] test]# CD Test6[[email protected] test6]# ll[[email protected] test6]# tar -zxvpf/opt/soft/test/ log31.tar.gz log2014.loglog2015.loglog2016.log[[email protected] test6]# ll Total0-rw-r--r--1 root root 0 11-13 06:03Log2014.log-rw-r--r--1 root root 0 11-13 06:06Log2015.log-rw-r--r--1 root root 0 11-16 14:41Log2016.log[[email protected] test6]#
Description
The-P property is important, especially if you want to keep the properties of the original file
This feature is also quite useful to be able to copy the required files as compressed files , file permissions, and any file information such as the date will be backed up. the information in the file does not change when you unzip it elsewhere
Example 6: In a folder, a new file is backed up than a date
Command:
Tar-n "2012/11/13"-ZCVF log17.tar.gz test
Output:
[Email protected] soft]# tar-n "2012/11/13"-zcvf log17.tar.gz testtar:treating Date '2012/ 11/13 ' as 2012-11-13 00:00:00 + 0 nanosecondstest/test/log31.tar.gztest/log2014.logtest/ Linklog.logtest/log2015.logtest/log2013.logtest/log2012.logtest/ Log2017.logtest/log2016.logtest/log30.tar.gztest/log.tartest/ Log.tar.bz2test/log.tar.gz
Description
Example 7: Backup folder contents are excluded from some files
Command:
Tar--exclude scf/service-zcvf scf.tar.gz scf/*
Output:
[Email protected] test]# tree scfscf|-- bin|-- Doc|-- lib '-- service '-- deploy |-- info '-- product7 directories, 0-- Exclude SCF/SERVICE-ZCVF scf.tar.gz scf/* scf/bin/scf/doc/scf/lib/[[email protected] test]#
Backup of the Scf/service directory is excluded
(RPM) One Linux command per day: Tar command