Details and use of tar commands for common linux Commands

Source: Internet
Author: User
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 the Linux tar, which is small but dirty.

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.

First, we need to clarify 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 functions:

Used to compress and decompress files. Tar itself does not have the compression function. It is implemented by calling the compression function.

3. command parameters:

Necessary parameters are as follows:

-A adds A compressed file to an existing compressed file.

-B. set the block size.

-C create a new compressed file

-D Record File difference

-R: add files to compressed files.

-U added and changed existing files to existing compressed files.

-X extract files from compressed files

-T display the compressed file content

-Z supports gzip file extraction

-J supports bzip2 file extraction

-Z supports compress file extraction

-V shows the operation process

-L file system boundary settings

-K keep the original file not to overwrite

-M: the reserved file is not overwritten.

-W confirm the correctness of the compressed file

The optional parameters are as follows:

-B: set the number of blocks.

-C switch to the specified directory

-F: specify the compressed file

-- Help: displays help information.

-- Version: displays version information.

4. common decompression/compression commands

Tar
Unpack: tar xvf FileName.tar
Package: tar cvf FileName.tar DirName
(Note: tar is packed, not compressed !)


. Gz
Decompress 1: gunzip FileName.gz
Decompress 2: gzip-d FileName.gz
Compression: gzip FileName

.Tar.gz and. tgz
Decompress: tar zxvf FileName.tar.gz
Compression: tar zcvf FileName.tar.gz DirName

. Bz2
Decompress 1: bzip2-d FileName.bz2
Decompress 2: bunzip2 FileName.bz2
Compression: bzip2-z FileName

.Tar.bz2
Decompress: tar jxvf FileName.tar.bz2
Compression: tar jcvf FileName.tar.bz2 DirName

. Bz
Extract 1: bzip2-d FileName. bz
Decompress 2: bunzip2 FileName. bz
Compression: unknown

. Tar. bz
Decompress: tar jxvf FileName.tar. bz
Compression: unknown

. Z
Decompress: uncompress FileName. Z
Compression: compress FileName

. Tar. Z
Decompress: tar Zxvf FileName.tar. Z
Compression: tar Zcvf FileName.tar. Z DirName

. Zip
Decompress: unzip FileName.zip
Compression: zip FileName.zip DirName

. Rar
Decompress: rar x FileName.rar
Compression: rar a FileName.rar DirName

 

5. use instances

Instance 1: pack all files into a tar package

Command:


Copy codeThe code is as follows:
Tar-cvf log.tar log2012.log
Tar-zcvf log.tar.gz log2012.log
Tar-jcvf log.tar.bz2 log2012.log

Output:


Copy codeThe code is as follows:
[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.log
Log2012.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 17:54 log.tar
-Rw-r -- 1 root 1413 11-29 :55 log.tar.bz2
-Rw-r -- 1 root 1413 11-29 :54 log.tar.gz

Note:

Tar-cvf log.tar log2012.log is only packaged and not compressed!
After packaging tar-zcvf log.tar.gz log2012.log, compress it with gzip
After packaging tar-zcvf log.tar.bz2 log2012.log, compress it with bzip2

The file File Name After parameter f is obtained by ourselves. we are used to using. tar for identification. 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.

Instance 2: Check the files in the preceding tar package.

Command:

Tar-ztvf log.tar.gz

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # tar-ztvf log.tar.gz
--- Xrw-r -- root/root 302108 06:03:25 log2012.log

Note:

Because we use gzip to compress log.tar.gz, to check the files in the log.tar.gz package, we need to add the z parameter.

Instance 3: extract the tar package

Command:

Tar-zxvf/opt/soft/test/log.tar.gz

Output:


Copy codeThe code is as follows: [root @ localhost test3] # ll
Total 0
[Root @ localhost test3] # tar-zxvf/opt/soft/test/log.tar.gz
Log2012.log
[Root @ localhost test3] # ls
Log2012.log
[Root @ localhost test3] #

Note:

By default, we can unbind the compressed files from anywhere.

Example 4: extract only some files in/tar

Command:


Copy codeThe code is as follows:
Tar-zxvf/opt/soft/test/log30.tar.gz log2013.log

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # tar-zcvf log30.tar.gz log2012.log log2013.log
Log2012.log
Log2013.log
[Root @ localhost test] # ls-al log30.tar.gz
-Rw-r -- 1 root 1512 11-30 log30.tar.gz
[Root @ localhost test] # tar-zxvf log30.tar.gz log2013.log
Log2013.log
[Root @ localhost test] # ll
-Rw-r -- 1 root 1512 11-30 log30.tar.gz
[Root @ localhost test] # cd test3
[Root @ localhost test3] # tar-zxvf/opt/soft/test/log30.tar.gz log2013.log
Log2013.log
[Root @ localhost test3] # ll
Total 4
-Rw-r -- 1 root 61 11-13 06:03 log2013.log
[Root @ localhost test3] #

Note:

I can use tar-ztvf to check the file name in the tar package. if you only need one file, you can decompress some files in this way!

Instance 5: back up the file and save its permissions

Command:


Copy codeThe code is as follows:
Tar-zcvpf log31.tar.gz log2014.log log2015.log log2016.log

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ll
Total 0
-Rw-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 14:41 log2016.log
[Root @ localhost test] # tar-zcvpf log31.tar.gz log2014.log log2015.log log2016.log
Log2014.log
Log2015.log
Log2016.log
[Root @ localhost test] # cd test6
[Root @ localhost test6] # ll
[Root @ localhost test6] # tar-zxvpf/opt/soft/test/log31.tar.gz
Log2014.log
Log2015.log
Log2016.log
[Root @ localhost test6] # ll
Total 0
-Rw-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 14:41 log2016.log
[Root @ localhost test6] #

Note:

This-p attribute is very important, especially when you want to retain the attributes of the original file

Instance 6: in a folder, only new files are backed up on a specified date.

Command:


Copy codeThe code is as follows:
Tar-N "2012/11/13"-zcvf log17.tar.gz test

Output:


Copy codeThe code is as follows:
[Root @ localhost soft] # tar-N "2012/11/13"-zcvf log17.tar.gz test
Tar: Treating date '2017/13 'as 2012/11 00:00:00 + 0 nanoseconds
Test/log31.tar.gz
Test/log2014.log
Test/linklog. log
Test/log2015.log
Test/log2013.log
Test/log2012.log
Test/log2017.log
Test/log2016.log
Test/log30.tar.gz
Test/log.tar
Test/log.tar.bz2
Test/log.tar.gz

Note:

Instance 7: The Backup folder contains excluded files.

Command:


Copy codeThe code is as follows:
Tar -- exclude scf/service-zcvf scf.tar.gz scf /*

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # tree scf
Scf
| -- Bin
| -- Doc
| -- Lib
'-- Service
'-- Deploy
| -- Info
'-- Product
7 directories, 0 files
[Root @ localhost test] # tar -- exclude scf/service-zcvf scf.tar.gz scf /*
Scf/bin/
Scf/doc/
Scf/lib/
[Root @ localhost test] #

Related Article

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.