Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

CVM: linux decompression command tar details

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Access to the server through SSH, it is inevitable to use compression, decompression, packaging, unpacking and so on, at this time the tar command is an essential and powerful tool. The most popular tar in linux is the sparrow, which is small, well-equipped and powerful.

The tar command creates files and directories for linux. With tar, you can create a file (backup file) for a particular file, change the file in the file, or add a new file to the file. Tar was originally used to create archives on tape, but now users can create archives on any device. With the tar command, you can package a large pile of files and directories into one file, which is useful for backing up files or combining several files into one file for network transfer.

First of all, we need to understand two concepts: packaging and compression. Packaging refers to turning a large pile of files or directories into a total file; compression is turning a large file into a small file through some compression algorithms.

Why distinguish between these two concepts? This is because many compression programs in Linux can compress only one file, so when you want to compress a lot of files, 56 Cloud reminds everyone that you have to pack this pile of files into a package first (tar command), and then compress it with a compression program (gzip bzip2 command). The most commonly used packaging program under linux is tar. Packages typed by tar programs are often called tar packages, and commands for tar package files usually end with .tar. After generating the tar package, you can use other programs to compress it.

1. Command format: tar [necessary parameters] [Select parameters] [File]

2. Command function: used to compress and decompress files. Tar itself does not have compression capabilities. It is implemented by calling the compression function.

3. Command parameters: the necessary parameters are as follows:

-An add compressed files to existing compressed files

-B sets the block size

-d differences in recording files

-r add a file to a compressed file

-u add changed and existing files to existing compressed files

-c create a new compressed file

-x extract files from compressed files

-v displays the operation process

-z supports gzip decompression of files

-t displays the contents of the compressed file

-l file system boundary settin

-j supports bzip2 decompression of files

-Z supports compress decompression of files

-k keep the original? File does not overwrite

-m keep the file from being 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 specifies the compressed file

-help displays help information

-version displays version information

4. Common decompression / compression commands

Tar

Unpack: tar xvf FileName.tar

Packaging: tar cvf FileName.tar DirName

(note: tar is packaging, not compression! )

.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

Decompress 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 an example

Example 1: package all files into tar packages

Command:

Tar-cvf log.tar log2012.log

Tar-zcvf log.tar.gz log2012.log

Tar-jcvf log.tar.bz2 log2012.log

Output:

[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.log

Log2012.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

Description:

Tar-cvf log.tar log2012.log is only packaged, not compressed!

After tar-zcvf log.tar.gz log2012.log is packaged, it is compressed in gzip

Tar-zcvf log.tar.bz2 log2012.log after packing? Compressed with bzip2

The file name after the parameter f is taken by ourselves, and we are used to using .tar for identification. If you add the z parameter, use .tar.gz or .tgz to represent the gzip compressed tar package; if you add the j parameter, use .tar.bz2 as the tar package name.

Example 2: check which files are in the above tar package. This is often used to see what is in the compressed package.

Command: tar-ztvf log.tar.gz

Output:

[root@localhost test] # tar-ztvf log.tar.gz

-xrw-r- root/root 302108 2012-11-13 06:03:25 log2012.log

Note: because we use gzip compressed log.tar.gz, we have to add the parameter z when we want to check the files in the log.tar.gz package.

Example 3: decompress the tar package

Life?: 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] # ls

Log2012.log

[root@localhost test3] #

Description:

Under default circumstances, we can unzip the compressed file anywhere.

Example 4: it is often used to extract only part of the files in / tar, and only extract some of the files inside. Add the name of the file to be extracted at the end.

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.log

Log2013.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.log

Log2013.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.log

Log2013.log

[root@localhost test3] # ll

Total 4

-rw-r-r- 1 root root 61 11-13 06:03 log2013.log

[root@localhost test3] #

Description: I can look up the file names in the tar package through tar-ztvf. If you only need one file, you can extract some of the files in this way!

Example 5: back up the file and save its permissions

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

Output:

[root@localhost test] # ll

Total 0

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

Description:

The attribute of this-p is very important, especially if you want to keep the attribute of the original file.

Description:

Example 6: the content of backup folder is to exclude some files

Command:

Tar zcvf data.tar.gz / data/-- exclude=/data/inc

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report