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

Linux Compression and Packaging tool gzip_bzip2_xz_zip_tar

2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Gizp:

* the gzip tool cannot compress directories, only files

Compression: gzip filename

[root@localhost test01] # ll-h * # View pre-compressed all.txt file size-rw-r--r-- 1 root root 4.2m September 7 13:44 all.txt [root@localhost test01] # gzip all.txt # compressed all.txt file [root@localhost test01] # ll-h * # View compressed all.txt file size-rw-r--r-- 1 root root 1.2m September 7 13:44 all.txt.gz

Decompress: gzip-d filename

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

Decompress: gunzip-d filename

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

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

[root@localhost test01] # gzip-9 all.txt [root@localhost test01] # file all.txt.gz # file View File the last column compression level is the maximum compression ratio 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 it)

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

-c parameter: keep the source file when compressing or decompressing

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

Bzip2:

* similar to gzip, directories cannot be compressed, only files can be compressed, and the compression ratio is higher than gzip.

Install the bzip2 tool:

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

Compression: bzip2 filename

Decompress: bizp2-d filename or bunzip2-d filename

View the contents of the compressed file: bzcat filename

* like gzip, the compression ratio can be specified, but the default compression level of bzip2 is 9, and the-c parameter can also be used.

Xz:

Similar to gzip and bzip2, directories cannot be compressed, only files can be compressed, and the compression ratio is higher than that of gzip and bzip2 *

Compression: xz filename

Decompress: xz-d filename or unxz-d filename

View the contents of the compressed file: xzcat filename

Like gzip and bzip, you can specify the compression ratio. The default compression level is the highest, and you can also use the-c parameter.

Gzip, bzip2 and xz use the-c parameter during decompression to not only retain the source file, but also rename the decompressed file *

Zip:

* zip can compress directories and files. You can specify the decompression path during decompression, but you cannot rename the decompression 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] # lsfiletest.txt test02 test.sh [root@localhost test01] # zipabc.zip filetest.txt test.sh adding: filetest.txt (deflated 85%) adding: test.sh (deflated 79%) [root@localhost test01] # ls # add two filetest.txt test.sh files to the compressed file abc.zipabc.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] # lsabc.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] # lsabc.zip filetest.txt linuxtest.zip test02 test.sh

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

Decompress: unzip filename

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

Extract the contents of the compressed file to the specified directory: unzip filename-d destination directory path

[root@localhost test01] # lsabc.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 one file (for example, to compress a directory with many small files, you can use tar to package the directory into a file and then compress it) to increase the transfer speed without changing the file size too much. When tar packages, you can package multiple directories and files at the same time.

Packaging: tar-cvf package filename source file

[root@localhost test01] # lstest02 test.sh [root@localhost test01] # tar-cvf testfile.tar test02/ test.sh test02/test02/all.txttest02/filetest.txttest.sh [root@localhost test01] # ls # package the directory / test02 and the file test.sh as the testfile.tar file test02 testfile.tar test.sh

Unpack: tar-xvf object file

[root@localhost test01] # lstest02 testfile.tar test.sh [root@localhost test01] # rm-rf test02 test.sh [root@localhost test01] # lstestfile.tar [root@localhost test01] # tar-xvf testfile.tar test02/test02/all.txttest02/filetest.txttest.sh [root@localhost test01] # lstest02 testfile.tar test.sh

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

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

Filter the specified file when packing:-- exclude

Filter the specified file:

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

Filter files of the specified type:

[root@localhost test01] # tar-cvf testfile.tar-- exclude "* .txt" test02/test02/ test02/test.sh [root@localhost test01] # lstest02 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] # lstest02 testfile.tar [root@localhost test01] # tar-tf testfile.tar test02/test02/all.txt

Tar supports compression while packaging:

1. Pack it and compress it into gzip package:-zcvf

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

Extract the tar.gz package:-zxvf

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

two。 Pack it and compress it into bzip2 package:-jcvf

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

Extract the tar.bz2 package:-jxvf

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

3. Pack it and compress it into xz package:-Jcvf

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

Extract the tar.xz package:-Jxvf

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

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