14-Linux压缩和解压类

gzip/gunzip

说明:

(1)只能压缩文件不能压缩目录
(2)不保留原来的文件
(3)同时多个文件会产生多个压缩包

gzip:压缩

[root@192 test]# ll # 压缩前查看
总用量 0
-rwxrwxrwx. 1 atguigu atguigu 0 1月   1 01:37 test.txt
[root@192 test]# gzip test.txt  #使用gzip将test.txt文件压缩
[root@192 test]# ll # 压缩后查看
总用量 4
-rwxrwxrwx. 1 atguigu atguigu 29 1月   1 01:37 test.txt.gz

 

gunzip:解压

# 继前面的压缩操作之后,直接进行解压
[root@192 test]# gunzip test.txt.gz # 解压操作 
[root@192 test]# ll # 解压后查看结果
总用量 0
-rwxrwxrwx. 1 atguigu atguigu 0 1月   1 01:37 test.txt

zip/unzip

说明:zip 压缩命令在windows/linux都通用,可以压缩目录且保留源文件。

zip

语法:zip 生成的压缩包文件名    目标文件1 目标文件2

参数:-r:压缩目录

# 案例一:压缩文件
[root@192 test]# touch 1.txt #创建两个文件用于压缩
[root@192 test]# touch 2.txt 
[root@192 test]# zip myzip.zip 1.txt 2.txt  #将1和2压缩,压缩后的文件名叫做:myzip.txt
  adding: 1.txt (stored 0%)
  adding: 2.txt (stored 0%)
[root@192 test]# ll # 查看压缩后的结果。源文件还在并产生了myzip文件
总用量 4
-rw-r--r--. 1 root root   0 1月   1 02:33 1.txt
-rw-r--r--. 1 root root   0 1月   1 02:33 2.txt
-rw-r--r--. 1 root root 298 1月   1 02:33 myzip.zip

# 案例二:使用-r参数压缩目录
[root@192 桌面]# ll # 压缩前有一个test目录
总用量 3720
drwxr-xr-x. 2 atguigu atguigu      49 1月   1 02:33 test

[root@192 桌面]# zip -r test.zip test  # 将test目录进行压缩
  adding: test/ (stored 0%)
  adding: test/1.txt (stored 0%)
  adding: test/2.txt (stored 0%)
  adding: test/myzip.zip (stored 0%)
[root@192 桌面]# ll # 查看压缩结果
总用量 3724
drwxr-xr-x. 2 atguigu atguigu      49 1月   1 02:33 test
-rw-r--r--. 1 root    root        910 1月   1 02:35 test.zip ###

unzip

语法:uzip 压缩包  [选项] 

选项:-d:解压后文件的存放目录

[root@192 桌面]# unzip test.zip -d /opt/ # 不加-d表示解压到当前目录,加了-d则要解压到指定的/opt目录下
Archive:  test.zip
   creating: /opt/test/
 extracting: /opt/test/1.txt         
 extracting: /opt/test/2.txt         
 extracting: /opt/test/myzip.zip     

tar:打包

说明:压缩后的文件格式为***.tar.gz

语法:tar [选项] xxx.tar.gz 要打包的文件

选项:

 案例一:压缩

[root@192 test]# ll
总用量 8
-rw-r--r--. 1 root root   0 1月   1 02:33 1.txt
-rw-r--r--. 1 root root   0 1月   1 02:33 2.txt
-rw-r--r--. 1 root root 298 1月   1 02:33 myzip.zip

[root@192 test]# tar -zcvf test.tar.gz 1.txt 2.txt  # 常用-zcvf
1.txt
2.txt
[root@192 test]# ll # 查看目录中已经产生了test.tar.gz文件
总用量 8
-rw-r--r--. 1 root root   0 1月   1 02:33 1.txt
-rw-r--r--. 1 root root   0 1月   1 02:33 2.txt
-rw-r--r--. 1 root root 298 1月   1 02:33 myzip.zip
-rw-r--r--. 1 root root 120 1月   1 02:55 test.tar.gz

案例二:解压

[root@192 test]# mkdir untar #创建一个目录用于存放解压后的文件
[root@192 test]# tar -zxvf test.tar.gz -C untar/ # 解压命令
1.txt
2.txt
[root@192 test]# ll untar/ # 查看指定目录中是否解压后的文件
总用量 0
-rw-r--r--. 1 root root 0 1月   1 02:33 1.txt
-rw-r--r--. 1 root root 0 1月   1 02:33 2.txt
[root@192 test]# ll # 查看当前目录中的文件,test.tar.gz没有消失
总用量 8
-rw-r--r--. 1 root root   0 1月   1 02:33 1.txt
-rw-r--r--. 1 root root   0 1月   1 02:33 2.txt
-rw-r--r--. 1 root root 298 1月   1 02:33 myzip.zip
-rw-r--r--. 1 root root 120 1月   1 02:55 test.tar.gz
drwxr-xr-x. 2 root root  32 1月   1 02:59 untar

 

posted @ 2024-01-21 01:22  邵杠杠  阅读(6)  评论(0编辑  收藏  举报