linux Compression and Packaging Tool gzip_bzip2_xz_zip_tar

gizp:
* The gzip tool can't compress directories, it can only compress files
Compression: gzip filename

[root@localhost test01]# ll -h *   #View all.txt file size before compression
-rw-r--r-- 1 root root 4.2M 9 July 13:44 all.txt
[root@localhost test01]# gzip all.txt   #Compress all.txt files
[root@localhost test01]# ll -h *    #View all.txt file size after compression
-rw-r--r-- 1 root root 1.2M 9 July 13:44 all.txt.gz

Unzip: gzip-d filename

[root@localhost test01]# ls
all.txt.gz
[root@localhost test01]# gzip -d all.txt.gz 
[root@localhost test01]# ls
all.txt

Unzip: gunzip-d filename

[root@localhost test01]# ls
all.txt.gz
[root@localhost test01]# gunzip -d all.txt.gz 
[root@localhost test01]# ls
all.txt

Specified compression rate: gzip-n filename (n range: 1-9, compression level 9 compression rate is the highest, compression speed is the slowest, the consumption of cup resources is relatively high, compression level 1 compression rate is the lowest, compression speed is the fastest, the consumption of cpu resources is relatively low, default level 6)

[root@localhost test01]# gzip -9 all.txt
[root@localhost test01]# file all.txt.gz  #file View file Last Column Compression Level is Maximum Compression Rate
all.txt.gz: gzip compressed data, was "all.txt", from Unix, last modified: Sat Sep  7 13:44:13 2019, max compression

View the contents of the compressed file: (use the zcat command to view the contents of the compressed file without decompressing)

[root@localhost test01]# zcat all.txt.gz 

- c parameter: Keep source files when compressing or decompressing

[root@localhost test01]# gzip -c all.txt > all.txt.gz
[root@localhost test01]# ls
all.txt  all.txt.gz
[root@localhost test01]# gzip -d -c all.txt.gz > all2.txt
[root@localhost test01]# ls
all2.txt  all.txt  all.txt.gz

bzip2:
* Similar to gzip, it can not compress directories, only compress files, and the compression rate is higher than gzip.
Install the bzip2 tool:

[root@localhost test01]# yum -y install bzip2

Compression: bzip2 filename
Decompression: bizp2-d filename or bunzip2-d filename
View the contents of the compressed file: bzcat filename
* Compression rate can be specified as gzip, but bzip2 has a default compression level of 9, and the - c parameter can also be used.

xz:
Similar to gzip and bzip2, it can not compress directories, only compress files, and the compression rate is higher than gzip and bzip2.*
Compression: xz filename
Decompression: xz-d filename or unxz-d filename
View the contents of the compressed file: xzcat filename
Like gzip and bzip, the compression rate can be specified. The default compression level is the highest, and the - c parameter can also be used.
gzip, bzip2, xz can not only preserve the source file, but also rename the decompressed file when decompressed with-c parameter*

zip:
* zip can compress directories and files, specify the decompression path when decompressing, but can not rename the decompressed content
Installation:

[root@localhost ~]# yum -y install zip

Compressed file: zip compressed file name source file name (source file can be multiple files)

[root@localhost test01]# ls
filetest.txt  test02  test.sh
[root@localhost test01]# zip abc.zip filetest.txt test.sh 
  adding: filetest.txt (deflated 85%)
  adding: test.sh (deflated 79%)
[root@localhost test01]# ls  #Add two files, filetest.txt test.sh, to the compressed file abc.zip
abc.zip  filetest.txt  test02  test.sh

Compressed directory: zip-r compressed file name source file name (source file can be multiple directories and files)

[root@localhost test01]# ls
abc.zip  filetest.txt  test02  test.sh
[root@localhost test01]# zip -r linuxtest.zip test02/ filetest.txt 
  adding: test02/ (stored 0%)
  adding: test02/all.txt (deflated 71%)
  adding: filetest.txt (deflated 85%)
[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh

* After zip compresses or decompresses files or directories, the source files are automatically retained

unzip filename

[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh
[root@localhost test01]# rm -rf filetest.txt test.sh 
[root@localhost test01]# ls
abc.zip  linuxtest.zip  test02
[root@localhost test01]# unzip abc.zip 
Archive:  abc.zip
  inflating: filetest.txt            
  inflating: test.sh                 
[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh

Extract the contents of the compressed file into the specified directory: unzip filename-d target directory path

[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh
[root@localhost test01]# unzip linuxtest.zip -d /root/mytest/
Archive:  linuxtest.zip
   creating: /root/mytest/test02/
  inflating: /root/mytest/test02/all.txt  
  inflating: /root/mytest/filetest.txt  
[root@localhost test01]# ls /root/mytest/
filetest.txt  test02

View the list of files in the compressed file: unzip-l filename
* Unlike gzip, bzip2 and xz, unzip can only view the list of files, not the contents of files.

[root@localhost test01]# unzip -l linuxtest.zip 
Archive:  linuxtest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  09-07-2019 15:18   test02/
  4340076  09-07-2019 13:44   test02/all.txt
     2943  09-07-2019 15:26   filetest.txt
---------                     -------
  4343019                     3 files

tar:
* The tar tool packages multiple files or directories into a single file (for example, to compress a directory, there are many small files, you can use tar to pack the directory into a file and then compress it), increase the transmission speed, the size of the file will not change too much, tar packages multiple directories and files at the same time.

Packing: tar-cvf packaged file name source file

[root@localhost test01]# ls
test02  test.sh
[root@localhost test01]# tar -cvf testfile.tar test02/ test.sh 
test02/
test02/all.txt
test02/filetest.txt
test.sh
[root@localhost test01]# ls  #Pack the directory / test02 and file test.sh as testfile.tar files
test02  testfile.tar  test.sh

Unpack: tar-xvf target file

[root@localhost test01]# ls
test02  testfile.tar  test.sh
[root@localhost test01]# rm -rf test02 test.sh 
[root@localhost test01]# ls
testfile.tar
[root@localhost test01]# tar -xvf testfile.tar 
test02/
test02/all.txt
test02/filetest.txt
test.sh
[root@localhost test01]# ls
test02  testfile.tar  test.sh

View the list of tar files: tar-tf target files

[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt
test02/filetest.txt
test.sh

Filtration of specified files when packing: - - exclude
Filter specified files:

[root@localhost test01]# ls test02/
all.txt  filetest.txt  test.sh
[root@localhost test01]# tar -cvf testfile.tar --exclude filetest.txt test02/ 
test02/
test02/all.txt
test02/test.sh
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt
test02/test.sh

Filter files of specified type:

[root@localhost test01]# tar -cvf testfile.tar --exclude "*.txt" test02/ 
test02/
test02/test.sh
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/test.sh

You can use multiple exclude:

[root@localhost test01]# tar -cvf testfile.tar --exclude filetest.txt --exclude test.sh test02/ 
test02/
test02/all.txt
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt

tar supports compression while packaging:

1. Compression into gzip package at the same time of packaging: -zcvf

[root@localhost test01]# du -sh test02/
4.2M    test02/
[root@localhost test01]# tar -zcvf testfile.tar.gz test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.gz 
1.2M    testfile.tar.gz

Unzip tar.gz package: -zxvf

[root@localhost test01]# tar -zxvf testfile.tar.gz 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

2. Compression into bzip2 package at the same time of packaging: -jcvf

[root@localhost test01]# tar -jcvf testfile.tar.bz2 test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.bz2 
1.2M    testfile.tar.bz2

Unzip tar.bz2 package: -jxvf

[root@localhost test01]# tar -jxvf testfile.tar.bz2 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

3. Compression into xz package at the same time of packaging: -Jcvf

[root@localhost test01]# tar -Jcvf testfile.tar.xz test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.xz 
252K    testfile.tar.xz

Unzip the tar.xz package: -Jxvf

[root@localhost test01]# tar -Jxvf testfile.tar.xz 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

Tags: Linux yum Unix

Posted on Sat, 07 Sep 2019 03:13:55 -0700 by racing_fire