Linux Command learning Note: Tar command

Source: Internet
Author: User
Tags bz2 unpack ssh access

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 the whole heap of files into a package (tar command), and then compress the compression program (gzipbzip2 command). The most common packaging program under Linux is tar, which is often referred to as a tar package, and the tar package file commands are usually used to make a package that uses a tar program.Tarend of. After the tar package is generated, you can use other programs to compress it. 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 function3. Command parameters: The necessary parameters are as follows:-A New compressed file to existing compression-B Set the chunk size-c Create a new compressed file-D. Differences in record files-r Add a file to a file that has been compressed-u add changed and existing files to already existing compressed files-x Extract files from compressed files-t display the contents of a compressed file-z support gzip unzip files-J Support Bzip2 Extract files-Z Support Compress extract files-v display operation process-L file system boundary settings-k Keep original file without overwriting-m reserved files are not overwritten-W confirm the correctness of the compressed file optional parameters are as follows:-b Set Number of blocks-C switch to the specified directory-f Specify compressed Files--Help Display assistance information--version displays release information4. Common decompression/Compress CommandTarUnpacking:TarXVF FileName.TarPackaging:TarCVF FileName.TarDirName (Note: Tar is packaged, not compressed!) ). GZ Decompression 1:Gunzipfilename.gz Decompression 2:gzip-d filename.gz Compression:gzipFileName.Tar. GZ and. tgz Decompression:TarZXVF FileName.Tar. GZ compression:TarZCVF FileName.Tar. GZ dirname.bz2 Decompression 1:bzip2-D filename.bz2 Decompression 2:bunzip2filename.bz2 Compression:bzip2-z FileName.Tar. bz2 Decompression:TarJXVF FileName.Tar. BZ2 compression:TarJCVF FileName.Tar. bz2 dirname.bz Decompression 1:bzip2-D filename.bz Decompression 2:bunzip2filename.bz compression: unknown.Tar. BZ Decompression:TarJXVF FileName.Tar. BZ compression: Unknown. Z Decompression: uncompress filename.z compression: Compress FileName.Tar. Z Decompression:TarZXVF FileName.Tar. Z-Compression:TarZCVF FileName.Tar. Z DirName.ZipUnzip:UnzipFileName.ZipCompression:ZipFileName.ZipDirname.rar Decompression: rar x filename.rar compression: rar a filename.rar DirName5. Using Instance Instance 1: Package The files all into a tar Package command:Tar-CVF log.TarLog2012.logTar-ZCVF log.Tar. GZ Log2012.logTar-JCVF log.Tar. bz2 log2012.log output: [[email protected] test]#ls-Al Log2012.log---xrw-r--1Root root302108  One- -  .:GenevaLog2012.log[[email protected] test]#Tar-CVF log.Tarlog2012.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--1Root root307200  One- in  -: WuLog.Tar-rw-r--r--1Root root1413  One- in  -: -Log.Tar. bz2-rw-r--r--1Root root1413  One- in  -: WuLog.Tar. GZ Description:Tar-CVF log.TarLog2012.log package only, do not compress! Tar-ZCVF log.Tar. GZ Log2012.log after packaging, togzipCompressionTar-ZCVF log.Tar. bz2 Log2012.log after packaging, tobzip2The file name that is compressed after the parameter f is taken by yourself, and we are used to it.Tarfor identification. If the z parameter is added, the.Tar. gz or. tgz to representgzipCompressed tar package, or if the J parameter is added.Tar. bz2 as the TAR package name. Example 2: Check which file commands are in the TAR package above:Tar-ZTVF log.Tar. GZ output: [[email protected] test]#Tar-ZTVF log.Tar. GZ---xrw-r--root/root302108  -- One- -  .:Geneva: -Log2012.log Description: As we usegzipThe compressed log.Tar. gz, so check log.TarIn the. GZ package, you have to add the Z parameter. Example 3: Unpack the TAR Package command:Tar-zxvf/opt/soft/test/log.Tar. GZ output: [[email protected] test3]# ll Total0[Email protected] test3]#Tar-zxvf/opt/soft/test/log.Tar. Gzlog2012.log[[email protected] test3]#lsLog2012.log[[email protected] test3] #说明: In the case of a preset, we can unpack the archive anywhere in instance 4: Just/Tarextract the commands from the partial files in: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--1Root root1512  One- -  ,: +Log30.Tar. Gz[[email protected] test]#Tar-ZXVF log30.Tar. GZ Log2013.loglog2013.log[[email protected] test]# ll-rw-r--r--1Root root1512  One- -  ,: +Log30.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--1Root root A  One- -  .:GenevaLog2013.log[[email protected] test3] #说明: I canTar-ZTVF to checkTarthe file name within the package, if only a single file, you can use this way to decompress some files! Example 5: File is backed down and its permissions command is saved:Tar-ZCVPF log31.Tar. GZ log2014.log log2015.log log2016.log output: [[email protected] test]# ll Total0-rw-r--r--1Root root0  One- -  .:GenevaLog2014.log-rw-r--r--1Root root0  One- -  .: .Log2015.log-rw-r--r--1Root root0  One- -  -: ALog2016.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--1Root root0  One- -  .:GenevaLog2014.log-rw-r--r--1Root root0  One- -  .: .Log2015.log-rw-r--r--1Root root0  One- -  -: ALog2016.log[[email protected] Test6] #说明: This-The properties of p are important, especially if you want to preserve the properties of the original file in instance 6: In the folder, the command is backed up by a new file than a date:Tar-N"2012/11/13"-ZCVF Log17.Tar. GZ Test output: [[email protected] soft]#Tar-N"2012/11/13"-ZCVF Log17.Tar. GZ TestTar: TreatingDate` -/ One/ -'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: Instance 7: Backup folder Contents is an exclude partial file 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 files[[email protected] test]# tar--exclude scf/service-zcvf scf.tar.gz scf/* SC F/bin/scf/doc/scf/lib/[[email protected] test]#

Linux Command learning Note: 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.