Linux常用的压缩和解压命令gzip,gunzip,tar,zip, unzip和bzip2,bunzip2

Linux常用的压缩和解压命令

1、压缩解压gzip和gunzip

特点:

  • 压缩比例大概为6:1

  • 该命令只能压缩文件,不能压缩目录

  • 压缩或者解压后不保留源文件

压缩示例:gzip 需要压缩的文件

解压示例gunzip xx.gz 或者gzip -d xx.gz

[root@localhost fengmin]# ll
total 656
-rw-r--r--. 1 root root 670293 Nov  4 10:16 services
[root@localhost fengmin]# gzip services 
[root@localhost fengmin]# ll
total 136
-rw-r--r--. 1 root root 136088 Nov  4 10:16 services.gz   # 压缩比例大概是6:1

[root@localhost fengmin]# ll
total 656
-rw-r--r--. 1 root root 670293 Nov  4 10:16 services
drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
[root@localhost fengmin]# gzip test   # 不能压缩目录
gzip: test is a directory -- ignored

2、压缩解压命令tar

打包目录或文件,如果参数不带-z,就是只打包不压缩。压缩后的文件格式.tar或者.tar.gz

打包格式:tar 选项[-zcf] 压缩后的文件名 目录

​ -c 打包

​ -v 显示详细的打包信息

​ -f 指定压缩后的文件名

​ -z 打包同时压缩

​ -x 解压

解包格式:tar

也可以分两步打包和压缩,tar -cvf xx.tar xx 然后再gzip xx.tar,生成的就是xx.tar.gz

# 一步到位,打包加压缩
[root@localhost fengmin]# ll
total 656
-rw-r--r--. 1 root root 670293 Nov  4 10:16 services
drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
[root@localhost fengmin]# tar -cvzf test.tar.gz test
test/
test/hello.txt

# 分两步,先打包再压缩
[root@localhost fengmin]# ll
total 656
-rw-r--r--. 1 root root 670293 Nov  4 10:16 services
drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
[root@localhost fengmin]# tar -cvf test.tar test/
test/
test/hello.txt
[root@localhost fengmin]# ll
total 668
-rw-r--r--. 1 root root 670293 Nov  4 10:16 services
drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
-rw-r--r--. 1 root root  10240 Nov  4 10:43 test.tar
[root@localhost fengmin]# gzip test.tar 
[root@localhost fengmin]# ll
total 660
-rw-r--r--. 1 root root 670293 Nov  4 10:16 services
drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
-rw-r--r--. 1 root root    154 Nov  4 10:43 test.tar.gz

# 解压
[root@localhost fengmin]# ll
total 1320
-rw-r--r--. 1 root root 670293 Nov  4 10:16 services
-rw-r--r--. 1 root root 675840 Nov  4 10:49 services.tar
drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
-rw-r--r--. 1 root root    145 Nov  4 10:46 test.tar.gz
[root@localhost fengmin]# tar -zxvf test.tar.gz 
test/
test/hello.txt

3、压缩解压命令zip

压缩文件或目录。linux和window都支持的格式

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

​ -r 压缩目录 (不带-r表示压缩文件)

unzip xxx.zip 解压文件

[root@localhost fengmin]# ll
total 4
-rw-r--r--. 1 root root 75 Nov  4 10:57 hello.txt
drwxr-xr-x. 2 root root 19 Nov  4 10:56 test
[root@localhost fengmin]# zip hello.zip hello.txt  # 压缩文件
  adding: hello.txt (deflated 77%)
[root@localhost fengmin]# ll
total 8
-rw-r--r--. 1 root root  75 Nov  4 10:57 hello.txt
-rw-r--r--. 1 root root 185 Nov  4 10:57 hello.zip
drwxr-xr-x. 2 root root  19 Nov  4 10:56 test
[root@localhost fengmin]# zip -r test.zip test/    # 压缩目录,要带上-r参数,否则就是压了一个空目录test
  adding: test/ (stored 0%)
  adding: test/hi.py (stored 0%)
[root@localhost fengmin]# ll
total 12
-rw-r--r--. 1 root root  75 Nov  4 10:57 hello.txt
-rw-r--r--. 1 root root 185 Nov  4 10:57 hello.zip
drwxr-xr-x. 2 root root  19 Nov  4 10:56 test
-rw-r--r--. 1 root root 308 Nov  4 10:58 test.zip

4、压缩解压命令bzip2和bunzip2

和gzip命令的区别就是多了-k参数,可以保留源文件,并且这个bzip2的压缩比惊人,压缩大文件推荐使用bzip2压缩。bzip2命令也是不能压缩目录啊,只能压缩文件

bzip2 选项[-k] [文件]

​ -k 产生压缩文件后保留源文件

压缩后文件格式.bz2

配合tar打包命令使用就是用-j参数

解压命令bunzip2 -k xx.bz2 解压,-k代表保留源文件

[root@localhost fengmin]# bzip2 -k hello.txt 
[root@localhost fengmin]# ll
total 8
-rw-r--r--. 1 root root 75 Nov  4 10:57 hello.txt
-rw-r--r--. 1 root root 50 Nov  4 10:57 hello.txt.bz2
drwxr-xr-x. 2 root root 19 Nov  4 10:56 test
[root@localhost fengmin]# bzip2 -k test/   # 不能压缩目录
bzip2: Input file test/ is a directory

[root@localhost fengmin]# ll
total 8
-rw-r--r--. 1 root root 75 Nov  4 10:57 hello.txt
-rw-r--r--. 1 root root 50 Nov  4 10:57 hello.txt.bz2
drwxr-xr-x. 2 root root 19 Nov  4 10:56 test
[root@localhost fengmin]# tar -cjf test.tar.bz2 test/   # -j参数代表bzip2压缩。解压是tar -xjf
[root@localhost fengmin]# ll
total 12
-rw-r--r--. 1 root root  75 Nov  4 10:57 hello.txt
-rw-r--r--. 1 root root  50 Nov  4 10:57 hello.txt.bz2
drwxr-xr-x. 2 root root  19 Nov  4 10:56 test
-rw-r--r--. 1 root root 140 Nov  4 11:13 test.tar.bz2

猜你喜欢

转载自blog.csdn.net/weixin_45455015/article/details/127698225