Linux common commands: tar command


  Accessing the server through SSH will inevitably require compression, decompression, packaging, unpacking, etc. At this time, the tar command is an essential and powerful tool. The most popular tar in linux is the sparrow, which is small, complete and powerful.

  The tar command can create archives for linux files and directories. With tar, you can create archives (backup files) for a particular file, change files in an archive, or add new files to an archive. Originally used to create archives on tape, tar can now be created 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 of all, two concepts must be clarified: packing and compression. Packing refers to turning a large number of files or directories into a total file; compression refers to turning a large file into a small file through some compression algorithms.

  Why distinguish these two concepts? This is due to the fact that many compression programs in Linux can only compress one file, so when you want to compress a lot of files, you have to first make a lot of files into a package (tar command), and then use Compressor to compress (gzip bzip2 command).

  The most commonly used package program under linux is tar. The package produced by the tar program is often called a tar package. The commands of a tar package file usually end with .tar. After the tar package is generated, it can be compressed by other programs.

1. Command format:

tar[required parameter][optional parameter][file] 

2. Command function:

  Used to compress and decompress files. tar itself has no compression capabilities. He is implemented by calling the compression function 

3. Command parameters:

The necessary parameters are as follows:

-A add archive to existing archive

-B set block size

-c create a new compressed file

-d log file differences

-r add files to already compressed files

-u add changed and existing files to existing archives

-x extract files from compressed files

-t show the contents of the compressed file

-z support gzip decompressing files

-j supports bzip2 decompression files

-Z supports compress to decompress files

-v show the operation process

-l filesystem boundary setting

-k keep the original file without overwriting

-m keep files from being overwritten

-W confirms the correctness of the compressed file

The optional parameters are as follows:

-b sets the number of blocks

-C switch to the specified directory

-f specifies the compressed file

--help display help information

--version display version information

4. Common decompression/compression commands

tar 
unpacking: tar xvf FileName.tar
packing: tar cvf FileName.tar DirName
(Note: tar is packing, not compression!)


.gz
decompress 1: gunzip FileName.gz
decompress 2: gzip -d FileName.gz
compress: gzip FileName

.tar.gz and .tgz
decompress: tar zxvf FileName.tar.gz
compress: tar zcvf FileName.tar.gz DirName

.bz2
decompress 1: bzip2 -d FileName.bz2
decompress 2: bunzip2 FileName.bz2
compress: bzip2 -z FileName

.tar.bz2
decompress: tar jxvf FileName.tar.bz2
compress: tar jcvf FileName.tar.bz2 DirName

.bz
decompress 1: bzip2 -d FileName.bz
decompress 2: bunzip2 FileName.bz
compress: unknown

.tar.bz
decompress: tar jxvf FileName.tar.bz
compress: unknown

.Z
decompress: uncompress FileName.Z
compress: compress FileName

.tar.Z
Unzip: tar Zxvf FileName.tar.Z
Compress: tar Zcvf FileName.tar.Z DirName

.zip
unzip: unzip FileName.zip
zip: zip FileName.zip DirName

.rar
unzip: rar x FileName.rar
zip: rar a FileName.rar DirName 

 

5. Example of use

Example 1: Package all files into tar packages

Order:

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

illustrate:

tar -cvf log.tar log2012.log only packs, not compresses! 

tar -zcvf log.tar.gz log2012.log packaged and compressed with gzip 

tar -zcvf log.tar.bz2 log2012.log is packaged and compressed with bzip2 

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

 

Example 2: Check which files are in the above tar package

Order:

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

illustrate:

  Since we use gzip compressed log.tar.gz, we need to add the z parameter when viewing the files in the log.tar.gz package.

 

Example 3: Unzip the tar package

Order:

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

illustrate:

  By default, we can unzip the compressed file anywhere

 

Example 4: Extract only part of the files in /tar

Order:

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

 illustrate:

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

 

Example 5: Back up the file and save its permissions

Order:

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

illustrate:

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

 

Example 6: In the folder, files newer than a certain date are backed up

Order:

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

output:

[root@localhost soft]# tar -N "2012/11/13" -zcvf log17.tar.gz test

tar: Treating date `2012/11/13' as 2012-11-13 00:00:00 + 0 nanoseconds

test/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

 illustrate:

 

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

Order:

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

output:

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

 

2018-05-02 16:09:01 in Nanjing

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128323&siteId=291194637