1. Description
The tar command is used to package or decompress files. The suffix of the packaged files is generally .tar.gz or .tgz
1.1 Packaging and compression
First of all, two concepts must be clarified: packing and compression. Packing refers to turning a large number of files or directories into a total file; compression refers to turning a large file into a small file through some compression algorithms.
Why distinguish these two concepts? This is due to the fact that many compression programs in Linux can only compress one file, so when you want to compress a large number of files, you have to first package the large number of files into a package (tar command), and then use Compressor to compress ( gzip bzip2 command).
2. Usage scenarios
2.1 Packaging
Pack
tar -cvf log.tar log2012.log 仅打包,不压缩!
tar -czvf log.tar.gz log2012.log 打包后,以 gzip 压缩
tar -cjvf log.tar.bz2 log2012.log 打包后,以 bzip2 压缩
package and save its permissions
tar -czvpf log31.tar.gz log2014.log log2015.log log2016.log
In the folder, files newer than a certain date are packaged
tar -N "2012/11/13" -zcvf log17.tar.gz logs/*
Exclude some files
tar --exclude scf/service -zcvf scf.tar.gz scf/*
2.2 Decompression
Check what files are in the tar package
tar -tzvf log.tar.gz
Unzip to the specified directory
tar -xzvf log.tar.gz -C /tmp/logs/
Unzip part of the file
tar -xzvf /opt/soft/test/log30.tar.gz log2013.log
3. Summary
压 缩:tar -czv -f filename.tar.gz 要被压缩的文件或目录名称
查 询:tar -tzv -f filename.tar.gz
解压缩:tar -xzv -f filename.tar.gz -C 欲解压缩的目录
4. Reference