One linux command per day: tar command

Keywords: Linux Attribute ssh network

Through SSH access to the server, it is inevitable to use compression, decompression, packaging, unpacking and so on. At this time, tar command is an indispensable powerful tool. The most popular tar in linux is sparrow, which is small, full of internal organs and powerful.

The tar command creates files for linux files and directories. With tar, you can create files (backup files) for a particular file, change files in the file, or add new files to the file. Tar was originally used to create files on tapes, but now users can create files on any device. With the tar command, a large number of files and directories can be packaged 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 understand two concepts: packaging 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 should we distinguish these two concepts? This comes from the fact that many compression programs in Linux can only compress one file, so when you want to compress a large number of files, you have to first group the files into a package (tar command), and then compress them with a compression program (gzip bzip2 command).

The most commonly used packaging program under linux is tar. Packets typed by tar are often called tar packages. The commands of tar package files usually end with. tar. After tar packages are generated, other programs can be used to compress them.

1. Command format:

tar [required parameters] [selection parameters] [files] 

2. Command function:

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

3. Command parameters:

The necessary parameters are as follows:

  • - A New Compressed File to Existing Compression
  • - B Set Block Size
  • - c Create a new compressed file
  • - d Differences in Recorded Files
  • - r Add files to compressed files
  • - u Add changed and existing files to existing compressed files
  • - x Extracts files from compressed files
  • - t Displays the contents of the compressed file
  • - z supports gzip decompression
  • - j supports bzip2 decompression files
  • - Z supports compress decompression
  • - v display operation process
  • - l File System Boundary Settings
  • - k Keep the original file uncovered
  • - m Reserved Files Are Not Overwritten
  • - W Confirms the Correctness of Compressed Files

The optional parameters are as follows:

  • - b Setting Block Number
  • - C Switches to the specified directory
  • - f Specifies compressed files
  • - help displays help information
  • Version displays version information

4. Common decompression/compression commands

tar 
Unpack: tar xvf FileName.tar
 Packing: tar cvf FileName.tar DirName
 (Note: tar is packaged, not compressed!)


.gz
 Unzip 1: gunzip FileName.gz
 Unzip 2: gzip-d FileName.gz
 Compression: gzip FileName

tar.gz and.tgz
 Unzip: tar zxvf FileName.tar.gz
 Compression: tar zcvf FileName.tar.gz DirName

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

.tar.bz2
 Unzip: tar jxvf FileName.tar.bz2
 Compression: tar jcvf FileName.tar.bz2 DirName

.bz
 Unzip 1: bzip2 -d FileName.bz
 Unzip 2: bunzip2 FileName.bz
 Compression: Unknown

.tar.bz
 Unzip: tar jxvf FileName.tar.bz
 Compression: Unknown

.Z
 Unzip: uncompress FileName.Z
 Compression: compress FileName

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

.zip
 Unzip: unzip FileName.zip
 Compression: zip FileName.zip DirName

.rar
 Unzip: rar x FileName.rar
 Compression: rar a FileName.rar DirName 

5. Use examples

Example 1: Packing 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

Explain:

Tar-cvf log. tar log2012. log is only packaged, not compressed!
Tar-zcvf log.tar.gz log2012.log packaged and compressed with gzip
Tar-zcvf log.tar.bz2 log2012.log packaged and compressed with bzip2
The filename after parameter f is self-selected, and we are used to using. tar as identification. If z parameter is added, then. tar.gz or. tgz is used to represent gzip compressed tar package; if j parameter is added, then. tar.bz2 is used as tar package name.

Example 2: See which files are in the tar package above
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

Explain:

Since we use gzip-compressed log.tar.gz, we need to add the parameter z to consult 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]#

Explain:

By default, we can unzip the archives anywhere.

Example 4: Unzip only some 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]#

Explain:

I can look up the name of the file in the tar package through tar-ztvf. If only one file is needed, some files can be decompressed through 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]#

Explain:

The attributes of this - p are important, especially when you want to keep the attributes of the original file.

Example 6: In a folder, new files are backed up before a certain date
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

Explain:

Example 7: Backup folder content excludes 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]#

Posted by JaclynM on Sun, 24 Mar 2019 16:48:29 -0700