Target Packing Compression Command for Linux

Preface:

The tar command itself has no compression function. It just packages multiple files or directories into one file, and the size of the packaged files is larger than the sum of the original file sizes. This will waste our disk space, which is not what we want in the working environment. If you want to pack multiple files or directories into one file and compress them, you can use the tar command in combination with the compression command. In practice, tar commands are usually used in combination with gzip and bzip2 compression formats.

1. Common parameters of tar command

parameter Meaning
j bzip2 compression format
z gzip compression format
c Create a new archive
x Unarchiving documents
v Detailed output
f file
t List the contents of the archive
C Specify a decompressed directory
X or -- exclude Exclusive Packaging

2. bzip2 compression format

Command format:

Compression command: tar jcvf xxx.tar.bz2 [file or directory]

2. Decompression command: tar jxvf xxxx. tar. bz2 - C. directory to store the decompressed files

Practical cases:

########compress########
[root@vms001 tar]# ls
001.txt  003.txt  005.txt    oldboy-02  oldboy.tar.bzip2  wahaha-0002
002.txt  004.txt  oldboy-01  oldboy-03  wahaha-0001       wahaha-0003
[root@vms001 tar]# rm -f oldboy.tar.bzip2 
[root@vms001 tar]# 
[root@vms001 tar]# tar jcvf oldboy.tar.bz2 *
001.txt
002.txt
003.txt
004.txt
005.txt
oldboy-01
oldboy-02
oldboy-03
wahaha-0001/
wahaha-0001/passwd
wahaha-0002/
wahaha-0002/group
wahaha-0003/
wahaha-0003/services
[root@vms001 tar]# ls
001.txt  003.txt  005.txt    oldboy-02  oldboy.tar.bz2  wahaha-0002
002.txt  004.txt  oldboy-01  oldboy-03  wahaha-0001     wahaha-0003
########decompression########
[root@vms001 tar]# ls
001.txt  003.txt  005.txt    oldboy-02  oldboy.tar.bz2  wahaha-0002
002.txt  004.txt  oldboy-01  oldboy-03  wahaha-0001     wahaha-0003
[root@vms001 tar]# ls /mnt/
[root@vms001 tar]# tar jxvf oldboy.tar.bz2 -C /mnt/   #- C. Specify a decompression storage directory
001.txt
002.txt
003.txt
004.txt
005.txt
oldboy-01
oldboy-02
oldboy-03
wahaha-0001/
wahaha-0001/passwd
wahaha-0002/
wahaha-0002/group
wahaha-0003/
wahaha-0003/services
[root@vms001 tar]# ls /mnt/
001.txt  003.txt  005.txt    oldboy-02  wahaha-0001  wahaha-0003
002.txt  004.txt  oldboy-01  oldboy-03  wahaha-0002

3. gzip compression format

Command format:

Compression command: tar zcvf xxx.tar.gz [file or directory]

2. Decompression command: tar zxvf x xxx.tar.gz-C decompression directory path

Practical cases:

###########compress###########
[root@vms001 tar]# ls
001.txt  003.txt  005.txt    oldboy-02  oldboy.tar.bz2  wahaha-0002
002.txt  004.txt  oldboy-01  oldboy-03  wahaha-0001     wahaha-0003
[root@vms001 tar]# tar zcvf oldboy_gzip.tar.gz *
001.txt
002.txt
003.txt
004.txt
005.txt
oldboy-01
oldboy-02
oldboy-03
oldboy.tar.bz2
wahaha-0001/
wahaha-0001/passwd
wahaha-0002/
wahaha-0002/group
wahaha-0003/
wahaha-0003/services
[root@vms001 tar]# ls
001.txt  004.txt    oldboy-02           oldboy.tar.bz2  wahaha-0003
002.txt  005.txt    oldboy-03           wahaha-0001
003.txt  oldboy-01  oldboy_gzip.tar.gz  wahaha-0002
###########decompression###########
[root@vms001 tar]# ls
001.txt  004.txt    oldboy-02           oldboy.tar.bz2  wahaha-0003
002.txt  005.txt    oldboy-03           wahaha-0001
003.txt  oldboy-01  oldboy_gzip.tar.gz  wahaha-0002
[root@vms001 tar]# ls /mnt/
[root@vms001 tar]# tar zxvf oldboy_gzip.tar.gz -C /mnt/
001.txt
002.txt
003.txt
004.txt
005.txt
oldboy-01
oldboy-02
oldboy-03
oldboy.tar.bz2
wahaha-0001/
wahaha-0001/passwd
wahaha-0002/
wahaha-0002/group
wahaha-0003/
wahaha-0003/services
[root@vms001 tar]# ls /mnt/
001.txt  003.txt  005.txt    oldboy-02  oldboy.tar.bz2  wahaha-0002
002.txt  004.txt  oldboy-01  oldboy-03  wahaha-0001     wahaha-0003

3. Excluding Compression Packaging

Exclude packing parameters:

    1,--exclude

    2,X

Practical cases:

###########Exclude a file package##########
[root@vms001 tar]# ll
//Total dosage 0
-rw-r--r--. 1 root root 0 7 month   9 16:19 001.txt
-rw-r--r--. 1 root root 0 7 month   9 16:19 002.txt
-rw-r--r--. 1 root root 0 7 month   9 16:19 003.txt
-rw-r--r--. 1 root root 0 7 month   9 16:19 004.txt
-rw-r--r--. 1 root root 0 7 month   9 16:19 005.txt
[root@vms001 tar]# tar jcvf txt.tar.bz2 ./* --exclude=./003.txt
./001.txt
./002.txt
./004.txt
./005.txt
###########Exclude multiple file packages##########
[root@vms001 tar]# ll
//Total dosage 4
-rw-r--r--. 1 root root  0 7 month   9 16:19 001.txt
-rw-r--r--. 1 root root  0 7 month   9 16:19 002.txt
-rw-r--r--. 1 root root  0 7 month   9 16:19 003.txt
-rw-r--r--. 1 root root  0 7 month   9 16:19 004.txt
-rw-r--r--. 1 root root  0 7 month   9 16:19 005.txt
-rw-r--r--. 1 root root 23 7 month   9 16:26 aa.txt
[root@vms001 tar]# tar jcvfX txt.tar.bz2 aa.txt ./*
./001.txt
./004.txt
./005.txt
[root@vms001 tar]# cat aa.txt 
aa.txt
002.txt
003.txt

Tip: When you need to exclude only one file package, you can use the -- exclude parameter to exclude the package. If you need to exclude multiple file packages, you can use the capitalized X parameter to package, put the name of the file to be excluded in a file, and then specify the file to exclude the package. For example, in the above case, the name of the file to be excluded is placed in the aa.txt file, which is designated as excluding package.

4. View the contents of the compressed file

Command format:

Tar tf xxx.tar.bz2 or xxx.tar.gz

Practical cases:

[root@vms001 tar]# ls
001.txt  002.txt  003.txt  004.txt  005.txt  aa.txt  txt.tar.bz2
[root@vms001 tar]# tar tf txt.tar.bz2 
./001.txt
./004.txt
./005.txt

5. Summary

Parameter combination:

1. bzip2 compression format:

Compression: jcvf

Decompression: jxvf

2. gzip compression format:

Compression: zcvf

Decompression: zxvf

3. View the contents of compressed files:

     tf

Exclude packing parameters:

1,--exclude

2,X


Keywords: Linux

Added by xdracox on Wed, 10 Jul 2019 00:07:21 +0300