Common Linux commands (25)-tar

Source: Internet
Author: User

Common Linux commands (25)-tar

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.

 

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 function:

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:

Required parameters:

-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

 

Optional parameters:

-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 package

Unpack: tar xvf FileName.tar

Package: tar cvf FileName.tar DirName

(Note: tar is packed, not compressed !)

 

. Gz package

Decompress 1: gunzip FileName.gz

Decompress 2: gzip-d FileName.gz

Compression: gzip FileName

 

.Tar.gz and. tgz packages

Decompress: tar zxvf FileName.tar.gz

Compression: tar zcvf FileName.tar.gz DirName


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

 

.Tar.bz2 package

Decompress: tar jxvf FileName.tar.bz2

Compression: tar jcvf FileName.tar.bz2 DirName


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

 

. Tar. bz package

Decompress: tar jxvf FileName.tar. bz

Compression: Unknown


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

 

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

 

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

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

 

 

5.Command instance

Instance 1. Pack all files into a tar package

Command:

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-jcvf log.tar.bz2 log2012.log, compress it with bzip2

Note: The 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.

 

[root@localhost test]# ls -al log2012.log---xrw-r-- 1 root 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--r-- 1 root root 307200 11-29 17:54 log.tar-rw-r--r-- 1 root root   1413 11-29 17:55 log.tar.bz2-rw-r--r-- 1 root root   1413 11-29 17:54 log.tar.gz

Instance 2. view the files in the tar package

 

Command: tar-ztvf log.tar.gz

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

 

[root@localhost test3]# ll[root@localhost test3]# tar -zxvf /opt/soft/test/log.tar.gzlog2012.log[root@localhost test3]# lslog2012.log[root@localhost test3]#

Instance 3. Only extract some files under/tar

 

Command: tar-zxvf/opt/soft/test/log30.tar.gz log2013.log

Note: I can use tar-ztvf to check the file name in the tar package. If only one file is required, I can decompress some files in this way!

 

[root@localhost test]# tar -zcvf log30.tar.gz log2012.log log2013.log log2012.loglog2013.log[root@localhost test]# ls -al log30.tar.gz -rw-r--r-- 1 root 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--r-- 1 root 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.loglog2013.log[root@localhost test3]# ll-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log[root@localhost test3]#

Instance 4. Back up the file and save its permissions

 

Command: tar-zcvpf log31.tar.gz log2014.log log2015.log log2016.log

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

 

[root@localhost test]# ll-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log-rw-r--r-- 1 root root      0 11-16 14:41 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.loglog2015.loglog2016.log[root@localhost test6]# ll-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log[root@localhost test6]#

 

 

Instance 5. In a folder, only new files are backed up on a specified date.

Command: tar-N "2012/11/13"-zcvf log17.tar.gz test

 

[root@localhost soft]# tar -N "2012/11/13" -zcvf log17.tar.gz testtar: Treating date `2012/11/13' 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

 

 

Instance 6. exclude some files when backing up folder content

Command: tar -- exclude scf/service-zcvf scf.tar.gz scf /*

 

[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]#

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.