Linux的压缩与打包

  • gzip 命令

gzip 1.txt 与 gunzip 1.txt.gz 

说明:gzip不支持压缩目录,gzip压缩文件后(不带参数)源文件不存在;

[root@localhost d6z]# ll 

-rw-r--r--. 1 root root 2376920 3月   1 09:04 1.txt

[root@localhost d6z]# gzip 1.txt

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 584162 3月   1 09:04 1.txt.gz

[root@localhost d6z]# gunzip 1.txt.gz 

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:04 1.txt

gzip 1.txt 与 gzip -d 1.txt.gz 

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:04 1.txt

[root@localhost d6z]# gzip 1.txt

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 584162 3月   1 09:04 1.txt.gz

[root@localhost d6z]# gzip -d 1.txt.gz 

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:04 1.txt

file 1.txt.gz  与 zcat 1.txt.gz

[root@localhost d6z]# file 1.txt.gz 

1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Mar  1 09:04:00 2018

[root@localhost d6z]# zcat 1.txt.gz |head -3

# Generated by NetworkManager

nameserver 119.29.29.29

# The upstream Mozilla.org project tests all changes to the root CA

gzip -c 1.txt > 1.txt.gz

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:04 1.txt

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

[root@localhost d6z]# 

[root@localhost d6z]# gzip -c 1.txt > 1.txt.gz

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:04 1.txt

-rw-r--r--. 1 root root  584162 3月   1 09:17 1.txt.gz

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

[root@localhost d6z]# 

gunzip -c 3.txt.gz  > ./3.txt

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:17 1.txt

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root       0 3月   1 09:18 3.txt

[root@localhost d6z]# gzip 3.txt

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:17 1.txt

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root      26 3月   1 09:18 3.txt.gz

[root@localhost d6z]# gunzip -c 3.txt.gz  > ./3.txt

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:17 1.txt

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root       0 3月   1 09:19 3.txt

-rw-r--r--. 1 root root      26 3月   1 09:18 3.txt.gz

  • bzip2 命令

安装命令

[root@localhost d6z]# bzip2 1.txt

-bash: bzip2: 未找到命令

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

已加载插件:fastestmirror

bzip2 1.txt 与 bunzip2 1.txt.bz2

[root@localhost d6z]# bzip2 1.txt

[root@localhost d6z]# ll

-rw-r--r--. 1 root root  131287 3月   1 09:17 1.txt.bz2

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root       0 3月   1 09:19 3.txt

-rw-r--r--. 1 root root      26 3月   1 09:18 3.txt.gz

[root@localhost d6z]# bunzip2 1.txt.bz2

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:17 1.txt

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root       0 3月   1 09:19 3.txt

-rw-r--r--. 1 root root      26 3月   1 09:18 3.txt.gz

说明:bzip2 与 gizp用法一致,不一一列举

  • xz 命令

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:17 1.txt

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root       0 3月   1 09:19 3.txt

-rw-r--r--. 1 root root      26 3月   1 09:18 3.txt.gz

[root@localhost d6z]# xz 1.txt

[root@localhost d6z]# ll

-rw-r--r--. 1 root root   27808 3月   1 09:17 1.txt.xz

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root       0 3月   1 09:19 3.txt

-rw-r--r--. 1 root root      26 3月   1 09:18 3.txt.gz

[root@localhost d6z]# unxz 1.txt.xz

[root@localhost d6z]# ll

-rw-r--r--. 1 root root 2376920 3月   1 09:17 1.txt

-rw-r--r--. 1 root root 2376920 3月   1 09:15 2.txt

-rw-r--r--. 1 root root       0 3月   1 09:19 3.txt

-rw-r--r--. 1 root root      26 3月   1 09:18 3.txt.gz

说明:xz 与 gizp用法一致,不一一列举

说明:gzip bzip2 xz 压缩程度依次增强,压缩参数默认为:6,参数级别:1-9。

  • zip 命令

说明:zip 与前面的用法相比,支持压缩目录,但是不支持查看压缩文件内容

zip -r zip_test.zip zip_test

[root@localhost ~]# zip -r zip_test.zip zip_test

  adding: zip_test/ (stored 0%)

  adding: zip_test/22/ (stored 0%)

  adding: zip_test/22/123.text (deflated 4%)

  adding: zip_test/2.text (stored 0%)

[root@localhost ~]# 

unzip zip_test.zip

[root@localhost ~]# unzip zip_test.zip  

Archive:  zip_test.zip

replace zip_test/22/123.text? [y]es, [n]o, [A]ll, [N]one, [r]ename: y

  inflating: zip_test/22/123.text    

replace zip_test/2.text? [y]es, [n]o, [A]ll, [N]one, [r]ename: A

 extracting: zip_test/2.text         

[root@localhost ~]# 

unzip zip_test.zip -d 20180305

[root@localhost ~]# unzip zip_test.zip -d 20180305

Archive:  zip_test.zip

   creating: 20180305/zip_test/22/

  inflating: 20180305/zip_test/22/123.text  

 extracting: 20180305/zip_test/2.text  

unzip -l zip_test.zip 

[root@localhost ~]# unzip -l zip_test.zip 

Archive:  zip_test.zip

  Length      Date    Time    Name

---------  ---------- -----   ----

        0  03-05-2018 08:36   zip_test/

        0  03-05-2018 08:36   zip_test/22/

       49  03-05-2018 08:36   zip_test/22/123.text

        7  03-05-2018 08:36   zip_test/2.text

---------                     -------

       56                     4 files

tree 20180305

[root@localhost ~]# tree 20180305

20180305

└── zip_test

    ├── 22

    │   └── 123.text

    └── 2.text

  • tar 命令

tar -cvf zip_test.tar zip_test

[root@localhost ~]# tar -cvf zip_test.tar zip_test

zip_test/

zip_test/22/

zip_test/22/123.text

zip_test/2.text

tar -xvf zip_test.tar 

[root@localhost ~]# tar -xvf zip_test.tar 

zip_test/

zip_test/22/

zip_test/22/123.text

zip_test/2.text

tar -tf zip_test.tar

[root@localhost ~]# tar -tf zip_test.tar

zip_test/

zip_test/22/

zip_test/22/123.text

zip_test/2.text

tar -cvf zip_test.tar --exclude "*.text"  zip_test    

说明:过滤 *.text 不压缩到tar包

[root@localhost ~]# tar -cvf zip_test.tar --exclude "*.text"  zip_test

zip_test/

zip_test/22/

[root@localhost ~]#

[root@localhost ~]# tar -tf zip_test.tar 

zip_test/

zip_test/22/

tar包与压缩

补充说明:tar包与压缩,解压命令类似

[root@localhost ~]# tar -czvf zip_test.tar.gz zip_test

zip_test/

zip_test/22/

zip_test/22/123.text

zip_test/2.text

[root@localhost ~]# du -sh zip_test.tar.gz 

4.0K    zip_test.tar.gz

[root@localhost ~]# tar -cjvf zip_test.tar.bz2  zip_test

zip_test/

zip_test/22/

zip_test/22/123.text

zip_test/2.text

[root@localhost ~]# du -sh zip_test.tar.bz2

4.0K    zip_test.tar.bz2

[root@localhost ~]# tar -cJvf zip_test.tar.xz  zip_test

zip_test/

zip_test/22/

zip_test/22/123.text

zip_test/2.text

[root@localhost ~]# du -sh zip_test.tar.xz

4.0K    zip_test.tar.xz

猜你喜欢

转载自my.oschina.net/u/3771523/blog/1627501