One linux Command every day (28): tar command

Source: Internet
Author: User
Tags gzip decompress touch command ssh access

One linux Command every day (28): tar command link: One linux Command every day (1): ls command http://www.bkjia.com/ OS /201210/163049.html#linuxlinuxcommand every day (2): cd command serial (3 ): pwd command Export (4): mkdir command Export (5): rm command http://www.bkjia.com/ OS /201210/161092.html#linuxcommand (6) every day: rmdir command http://www.bkjia.com/ OS /201210/164017. Html; one linux Command every day (7): mv command running (8): cp command http://www.bkjia.com/ OS /201210/163164.html#a linuxcommand every day (9): touch command running (10): cat command running (11 ): nl command http://www.bkjia.com/ OS /201211/165990.htmla linuxcommand every day (12): more command http://www.bkjia.com/ OS /201211/165994.htmllinuxcommand every day (13 ): Less command example (14): head command example (15): tail command http://www.bkjia.com/ OS /201211/168702.htmllinuxcommand every day (16): which command example (17): whereis command example (18 ): locate command http://www.bkjia.com/ OS /201211/168895.htmla linuxcommand every day (19): find command overview ht Tp: // timeout (20): exechttp of the find command: // www.bkjia.com/ OS /201211/168901.htmllinuxcommand (21) every day: xargshttp of the find command: // timeout (22 ): detailed description of the parameters of the find command: http://www.bkjia.com/ OS /201211/168912.htmla linuxcommand every day (23): Linux directory structure (24): Linux file type and extension (25 ): Linux File Attribute details examples (26): Using SecureCRT to upload and download files http://www.bkjia.com/ OS /201211/172022.htmlevery day a linuxlinuxcommand (27): linux chmod command http://www.bkjia.com/ OS /201211/172028.html through SSH access server, will inevitably use compression, decompression, in this case, the tar command is an essential and powerful tool. The most popular tar in linux is very powerful, although it is small. The tar command can create files for linux Files And Directories. Using tar, you can create a file (backup file) for a specific file, change the file in the file, or add a new file to the file. Tar was originally used to create files on tape. Now, you can create files on any device. Using the tar command, you can package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission. Www.2cto.com has two concepts: Packaging and compression. Packaging refers to converting a large number of files or directories into a total file. Compression refers to converting a large file into a small file through some compression algorithms. Why do we need to differentiate these two concepts? This is because many Linux compression programs can only compress one file. In this way, when you want to compress a large number of files, you must first compress these files into a package (tar command ), then use the compression program to compress (gzip bzip2 command ). At the end of lifecycle. After the tar package is generated, other programs can be used for compression. 1. Command Format: tar [required parameters] [select parameters] [files] 2. Command function: Used to compress and decompress files. Tar itself does not have the compression function. It is implemented by calling the compression function. command parameters: the required parameters are as follows: -A adds A compressed file to an existing compressed file-B sets the block size-c creates A new compressed file-d records the file difference-r adds the file to the compressed file-u add change and existing files to existing compressed files-x Extract files from compressed files-t display compressed file content-z support gzip decompress files-j support bzip2 decompress files- Z supports compress decompression file-v display operation process-l file system boundary settings-k keep original file not overwrite-m keep file not overwrite-W confirm compression file correctness optional parameters are as follows: -B: set the number of blocks.-C: switch to the specified directory.-f: Specify the compressed file. -- help: display the help information. -- version: display the version information. 4. common extract/compress command tar unpack: tar xvf FileName.tar package: tar cvf FileName.tar DirName (Note: tar is packed, 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 nvidirname. bz extract 1: bzip2-d FileName. bz extract 2: bunzip2 FileName. bz compression: Unknown .tar. bz decompress: tar jxvf FileName.tar. bz compression: unknown. Z decompression: uncompress F IleName. Z compression: compress FileName.tar. Z decompress: tar Zxvf FileName.tar. Z compression: tar Zcvf FileName.tar. Z DirName.zip unzip: unzip FileName.zip compression: zip FileName.zip DirName. rar decompression: rar x FileName.rar compression: rar a FileName.rar DirName 5. use instance 1: package all files into a tar package command: tar-cvf log.tar log2012.logtar-zcvf log.tar.gz log2012.logtar-jcvf log.tar.bz2 log2012.log output: [root @ localhost test] # ls-al log2012.log --- xrw-r-1 root 302108 11-13 06:03 log2012.log [root @ localhost test] # tar-cvf log.tar log2012.log log2012.log [root @ localhost test] # tar-zcvf log.tar.gz log2012.loglog2012. log [root @ localhost test] # tar-jcvf log.tar.bz2 log2012.log log2012.log [root @ localhost test] # ls-al *. tar *-rw-r -- 1 root 307200 11-29 log.tar-rw-r -- 1 root 1413 11-29 log.tar.bz2-rw-r -- r -- 1 root 1413 11-29 log.tar. g Z Note: tar-cvf log.tar log2012.log is only packaged and not compressed! After packaging tar-zcvf log.tar.gz log2012.log, compress tar-zcvf log.tar.bz2 log2012.log with gzip, compress the file name after the bzip2 parameter f with your own name, which is used in our habits. tar. If the z parameter is added, .tar.gz or. tgz is used to represent the compressed tar package of gzip. If the j parameter is added, .tar.bz2 is used as the tar package name. Example 2: Check the file commands in the tar package: tar-ztvf log.tar.gz output: [root @ localhost test] # tar-ztvf log.tar.gz --- xrw-r -- root/root 302108 06:03:25 log2012.log Description: Because we use gzip compressed log.tar.gzto check the files in the log.tar.gz package, you have to add the z parameter. Example 3: extract the tar package with the command tar-zxvf/opt/soft/test/log.tar.gz. Output: [root @ localhost test3] # ll total 0 [root @ localhost test3] # tar-zxvf/opt/soft/test/log.tar.gz log2012.log [root @ localhost test3] # lslog2012.log [root @ localhost test3] # description: under the Preset conditions, we can unmount the compression file anywhere. Example 4: only extract some files in/tar. command: tar-zxvf/opt/soft/test/log30.tar.gz log2013.log output: [root @ localhost test] # tar-zcvf log30.tar.gz log2012.log log2013.log log2012.logl Og2013.log [root @ localhost test] # ls-al log30.tar.gz-rw-r -- 1 root 1512 11-30 08:19 log30.tar.gz [root @ localhost test] # tar-zxvf log30.tar.gz log2013.loglog2013. log [root @ localhost test] # ll-rw-r -- 1 root 1512 11-30 08:19 log30.tar.gz [root @ localhost test] # cd test3 [root @ localhost test3] # tar-zxvf/opt/soft/test/log30.tar.gz log2013.logloglog2013. log [root @ localhost test3] # ll total 4-rw-r -- R -- 1 root 61 11-13 log2013.log [root @ localhost test3] # Note: I can check the file name in the tar package through tar-ztvf, if you only need one file, you can decompress some files in this way! Instance 5: Back up the file and save its permission command: tar-zcvpf log31.tar.gz log2014.log log2015.log log2016.log output: [root @ localhost test] # ll total 0-rw-r -- r -- 1 root 0 11-13 06:03 log2014.log-rw-r -- 1 root 0 11-13 06:06 log2015.log-rw -r -- 1 root 0 11-16 log2016.log [root @ localhost test] # tar-zcvpf log31.tar.gz log2014.log log2015.log log2016.log log2014.loglog2015. loglog2016.log [root @ localhost test] # cd test6 [root @ localhost test6] # ll [root @ localhost test6] # tar-zxvpf/opt/soft/test/log31.tar.gz log2014.logloglog2015. loglog2016.log [root @ localhost test6] # ll total 0-rw-r -- r -- 1 root 0 11-13 03 log2014.log-rw-r -- 1 root 0 11-13 06:06 log2015.log- rw-r -- 1 root 0 11-16 log2016.log [root @ localhost test6] # description: this-p attribute is very important, especially when you want to retain the attributes of the original file instance 6: In the folder, the BACKUP command is only available for files newer than a date: tar-N "2012/11/13"-zcvf log17.tar.gz test output: [root @ localhost soft] # tar-N "2012/11/13"-zcvf log17.tar.gz testtar: treating date '2017/13 'as 2012/11 00:00:00 + 0 nanosecondstest/test/log31.tar.gz test/log2014.logtest/linklog. logtest/log2015.logtest/log2013.logtest/log2012.logtest/log2017.logtest/log2016.logtest/log30.tar.gz test/log.tar test/release test/log.tar.gz Description: instance 7: The backup folder contains the command to exclude some files: tar -- exclude scf/service-zcvf scf.tar.gz scf/* output: [root @ localhost test] # tree scfscf | -- bin | -- doc | -- lib '-- service' -- deploy | -- info' -- product7 directories, 0 files [root @ localhost test] # tar -- exclude scf/service-zcvf scf.tar.gz scf/* scf/bin/scf/doc/scf/lib/[root @ localhost test] #

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.