欢迎光临
我们一直在努力

Linux常用命令之压缩解压命令

gzip命令用于压缩文件,英文原意为GNU zip,所在路径/bin/gzip,其语法格式为:

gzip [文件]

压缩后的文件格式为.gz。

例:将/etc目录下的services文件拷贝至/tmp目录,并采用gzip进行压缩。

[root@localhost tmp]# cp /etc/services services

[root@localhost tmp]# ls

services

[root@localhost tmp]# gzip services

[root@localhost tmp]# ls

services.gz

注:gzip只能压缩文件,无法压缩目录;gzip命令不保留源文件,上例就可以看出。

(2)gunzip
gunzip命令用于解压缩文件,英文原意为GNU unzip,所在路径为/bin/gunzip,其语法格式为:

gunzip [压缩文件]

例:解压缩新创建的压缩文件

[root@localhost tmp]# gunzip services.gz

[root@localhost tmp]# ls

services

注:也可以使用gzip -d解压缩文件;gunzip执行后不保留源文件。

(3)tar
tar命令用于打包目录,所在路径为/bin/tar,其语法格式为:

tar 选项[-zcf] [压缩后的文件名] [目录]

其中,-c选项表示打包;-v选项表示显示详细信息,-f选项指定文件名,-z选项表示打包的同时进行压缩,压缩后的文件格式为.tar.gz。

例:在/tmp目录下创建test/test1和test/test2目录,然后对test目录进行打包。

[root@localhost tmp]# mkdir -p test/test1 test/test2

[root@localhost tmp]# tar -czvf test.tar.gz test

test/

test/test2/

test/test1/

[root@localhost tmp]# ls -l

total 636

-rw-r--r--. 1 root root 641020 Jan  1 22:17 services

drwxr-xr-x. 4 root root   4096 Jan  1 22:25 test

-rw-r--r--. 1 root root    134 Jan  1 22:26 test.tar.gz

tar解压缩的语法格式为:

tar -xzvf [压缩文件]

其中,-x选项表示解包,-v选项表示显示详细信息,-f选项表示指定解压文件,-z选项表示解压缩。

例:删除创建的test目录,然后解压缩test.tar.gz

[root@localhost tmp]# rm -rf test

[root@localhost tmp]# ls

services  test.tar.gz

[root@localhost tmp]# tar -xzvf test.tar.gz

test/

test/test2/

test/test1/

[root@localhost tmp]# ls

services  test  test.tar.gz

(4)zip
zip命令用于压缩文件或目录,所在路径为/usr/bin/zip,其语法格式为:

zip 选项[-r] [压缩后的文件名] [文件或目录]

其中,-r选项表示压缩目录,压缩后的文件格式为.zip。

例:将services文件进行zip压缩:

[root@localhost tmp]# zip services.zip services

  adding: services (deflated 80%)

[root@localhost tmp]# ls

services  services.zip  test  test.tar.gz

注:zip格式在Linux和Windows中均存在;压缩比并不是很高。

(5)unzip
unzip命令用于解压.zip格式的文件,所在路径为/usr/bin/unzip,其语法格式为:

unzip [压缩文件]

例:解压services.zip文件

[root@localhost tmp]# unzip services.zip

Archive:  services.zip

replace services? [y]es, [n]o, [A]ll, [N]one, [r]ename:

源文件存在,解压时询问是否替换。

(6)bzip2
bzip2是gzip的升级版本,也用于压缩文件,所在路径为/usr/bin/bzip2,其语法格式为:

bzip2 选项[-k] [文件]

其中-k选项产生压缩文件后保留源文件。压缩后的文件格式为.bz2。

使用tar命令可以将目录保存为.bz2文件,其格式为:

tar 选项[-cjvf] [压缩后文件名] [目录]

其中-j选项即表示压缩为.tar.bz2文件。

例:使用bzip2命令对services文件进行压缩,随后将test目录也进行压缩

[root@localhost tmp]# bzip2 -k services

[root@localhost tmp]# ls

services  services.bz2  services.zip  test  test.tar.gz

[root@localhost tmp]# tar -cjvf test.tar.bz2 test

test/

test/test2/

test/test1/

[root@localhost tmp]# ls

services  services.bz2  services.zip  test  test.tar.bz2  test.tar.gz

(7)bunzip2
bunzip2命令用于解压缩.bz2文件,所在路径为/usr/bin/bunzip2,其语法格式为:

bunzip2 选项[-k] [压缩文件]

其中-k表示解压缩后保留源文件。

使用tar命令也可以解压缩.bz2的文件,其格式为:

tar 选项[-xjvf] [压缩文件]

例:删除services文件和test文件夹,然后分别解压缩:

[root@localhost tmp]# rm -rf services test

[root@localhost tmp]# ls

services.bz2  services.zip  test.bz2  test.tar.gz

[root@localhost tmp]# bunzip2 services.bz2

[root@localhost tmp]# tar -xjvf test.tar.bz2

test/

test/test2/

test/test1/

[root@localhost tmp]# ls

services  services.zip  test  test.tar.bz2  test.tar.gz
赞(1)

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0

    11111111

    微博客7年前 (2017-10-12)回复

微助手微博客--关注互联网

联系我们我们