One linux command every day (28): tar command

Source: Internet
Author: User
Tags gzip decompress touch command
One linux command every day (28): tar command link: One linux command every day (1): ls command http://www.2cto.com/os/201210/163049.html ; One linux command every day (2): cd command http://www.2cto.com/os/201210/163050.html A linux command every day (28): tar command link: A linux command every day (1): ls command http://www.2cto.com/os/201210/163049.html ; One linux command every day (2): cd command http://www.2cto.com/os/201210/163050.html ; One linux command every day (3): pwd command http://www.2cto.com/os/201210/163462.html ; One linux command every day (4): mkdir command http://www.2cto.com/os/201210/163463.html ; One linux command every day (5): rm command http://www.2cto.com/os/201210/163662.html ; One linux command (6) every day: rmdir command http://www.2cto.com/os/201210/164017.html ; One linux command (7) every day: mv command http://www.2cto.com/os/201210/164247.html ; One linux command every day (8): cp command http://www.2cto.com/os/201210/164254.html ; One linux command every day (9): touch Command http://www.2cto.com/os/201211/165699.html ; One linux command every day (10): cat command http://www.2cto.com/os/201211/165989.html ; One linux command every day (11): nl command http://www.2cto.com/os/201211/165990.html One linux command every day (12): more command http://www.2cto.com/os/201211/165994.html One linux command every day (13): less command http://www.2cto.com/os/201211/165998.html One linux command every day (14): head Command http://www.2cto.com/os/201211/166191.html One linux command every day (15): tail command http://www.2cto.com/os/201211/168702.html One linux command every day (16): which command http://www.2cto.com/os/201211/168890.html A linux command (17) every day: whereis command http://www.2cto.com/os/201211/168893.html One linux command (18) every day: locate command http://www.2cto.com/os/201211/168895.html One linux command every day (19): find command overview http://www.2cto.com/os/201211/168897.html One linux command every day (20): find command exec http://www.2cto.com/os/201211/168901.html One linux command (21) every day: find command xargs http://www.2cto.com/os/201211/168903.html A linux command (22) every day: detailed description of the parameters of the find Command http://www.2cto.com/os/201211/168912.html A linux command (23) every day: Linux directory structure http://www.2cto.com/os/201211/170430.html One linux command every day (24): Linux file type and extension http://www.2cto.com/os/201211/170431.html One linux command every day (25): Explanation of linux file attributes http://www.2cto.com/os/201211/170434.html One linux command every day (26): use SecureCRT to upload and download files http://www.2cto.com/os/201211/172022.html One linux command every day (27): linux chmod command http://www.2cto.com/os/201211/172028.html To access the server through SSH, it is inevitable that compression, decompression, packaging, and unpacking will be used. 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.