tar、gzip、zip压缩命令

which命令

查找PATH环境变量中的文件,Linux内置命令不在path中

[root@timy-test-k8s01 ~]# which python
/usr/bin/python

whereis命令

whereis命令用来定位指定的二进制程序、源代码文件和man手册页等相关文件的路径
[root@timy-test-k8s01 ~]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz

tar命令

tar命令在Linux系统里,可以实现对多个文件进行,压缩、打包、解包

打包:将一大堆文件或目录汇总成一个整体

压缩:将大文件压缩成小文件,节省磁盘空间

 

语法:

tar        (选项) (参数)

 

-A或--catenate:        新增文件到以存在的备份文件;

-B:        设置区块大小;

-c或--create:        建立新的备份文件;

-C<目录>:        这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项;

-d:        记录文件的差别;

-x或--extract或--get:        从备份文件中还原文件;

-t或--list:        列出备份文件的内容;

-z或--gzip或--ungzip:        通过gzip指令处理备份文件;

-Z或--compress或--uncompress:        通过compress指令处理备份文件

-f<备份文件>或--file=<备份文件>:        指定备份文件;

-v或--verbose:        显示指令执行过程;

-r:        添加文件到已经压缩的文件

-u:        添加改变了和现有的文件到已经存在的压缩文件;

-j:        支持bzip2解压文件;

-v:        显示操作过程

-l:        文件系统边界设置;

-k:        保留原有文件不覆盖

-m:        保留文件不被覆盖

-w:        确认压缩文件的正确性

-p或--same-permissions:        用原来的文件权限还原文件

-p或--absoulte-names:        文件名使用绝对名称,不移除文件名称前的"/"号;不建议使用

-N <日期格式> 或 --newer=<日期时间>:        只将较指定日期更新的文件保存到备份文件里

--exclude=<范本样式>:        排除符合范本样式的文件

-h        --dereference跟踪符号链接;将它们所指向的文件归档并输出

案例

1.仅打包,不压缩

#tar        参数        包裹文件名        需要打包的文件

tar        -cvf        alltemp.tar        ./*

[root@timy-test-k8s01 Desktop]# tar -cvf alltemp.tar ./*
./0test/
./1test/
./2test/
./3test/
./4test/
./5test/
./6test/
./7test/
./8test/
./9test/
./alex.txt
./cesh1.txt
./ceshi1.txt
./ceshi.xt
./mjj.txt
./test.sh
./test.txt

[root@timy-test-k8s01 Desktop]# ll
total 44
drwxr-xr-x. 2 root root     6 Apr 23 01:45 0test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 1test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 2test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 3test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 4test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 5test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 6test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 7test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 8test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 9test
-rw-r--r--. 1 root root    17 Apr 22 05:25 alex.txt
-rw-r--r--. 1 root root 20480 Apr 23 21:56 alltemp.tar
-rw-r--r--. 1 root root    14 Apr 16 04:32 cesh1.txt
-rw-r--r--. 1 root root    39 Apr 16 04:36 ceshi1.txt
-rw-r--r--. 1 root root     0 Apr 16 05:31 ceshi.xt
-rw-r--r--. 1 root root    14 Apr 19 21:34 mjj.txt
-rwxr-xr-x. 1 root root    66 Apr 16 04:14 test.sh
-rw-r--r--. 1 root root    43 Apr 23 02:55 test.txt

2.对Desktop下的所有文件进行打包并压缩,节省磁盘空间

tar        -czvf        alltemp1.tar.gz        ./*        #./*代表当前目录下的所有文件

[root@timy-test-k8s01 Desktop]# tar -czvf alltemp1.tar.gz ./*
./0test/
./1test/
./2test/
./3test/
./4test/
./5test/
./6test/
./7test/
./8test/
./9test/
./alex.txt
./alltemp.tar
./cesh1.txt
./ceshi1.txt
./ceshi.xt
./mjj.txt
./test.sh
./test.txt
[root@timy-test-k8s01 Desktop]# ll
total 48
drwxr-xr-x. 2 root root     6 Apr 23 01:45 0test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 1test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 2test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 3test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 4test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 5test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 6test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 7test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 8test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 9test
-rw-r--r--. 1 root root    17 Apr 22 05:25 alex.txt
-rw-r--r--. 1 root root   656 Apr 23 22:02 alltemp1.tar.gz
-rw-r--r--. 1 root root 20480 Apr 23 21:56 alltemp.tar
-rw-r--r--. 1 root root    14 Apr 16 04:32 cesh1.txt
-rw-r--r--. 1 root root    39 Apr 16 04:36 ceshi1.txt
-rw-r--r--. 1 root root     0 Apr 16 05:31 ceshi.xt
-rw-r--r--. 1 root root    14 Apr 19 21:34 mjj.txt
-rwxr-xr-x. 1 root root    66 Apr 16 04:14 test.sh
-rw-r--r--. 1 root root    43 Apr 23 02:55 test.txt

3.解压缩文件,将之前的压缩的文件解压到Desktop路径

tar        -xzvf        alltemp1.tar.gz

[root@timy-test-k8s01 Desktop]# tar -xzvf alltemp1.tar.gz 
./0test/
./1test/
./2test/
./3test/
./4test/
./5test/
./6test/
./7test/
./8test/
./9test/
./alex.txt
./alltemp.tar
./cesh1.txt
./ceshi1.txt
./ceshi.xt
./mjj.txt
./test.sh
./test.txt
[root@timy-test-k8s01 Desktop]# ll
total 48
drwxr-xr-x. 2 root root     6 Apr 23 01:45 0test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 1test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 2test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 3test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 4test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 5test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 6test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 7test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 8test
drwxr-xr-x. 2 root root     6 Apr 23 01:45 9test
-rw-r--r--. 1 root root    17 Apr 22 05:25 alex.txt
-rw-r--r--. 1 root root   656 Apr 23 22:02 alltemp1.tar.gz
-rw-r--r--. 1 root root 20480 Apr 23 21:56 alltemp.tar
-rw-r--r--. 1 root root    14 Apr 16 04:32 cesh1.txt
-rw-r--r--. 1 root root    39 Apr 16 04:36 ceshi1.txt
-rw-r--r--. 1 root root     0 Apr 16 05:31 ceshi.xt
-rw-r--r--. 1 root root    14 Apr 19 21:34 mjj.txt
-rwxr-xr-x. 1 root root    66 Apr 16 04:14 test.sh
-rw-r--r--. 1 root root    43 Apr 23 02:55 test.txt

4.列出压缩文件中的内容

tar        -ztvf        alltmp1.tar.gz

[root@timy-test-k8s01 Desktop]# tar -ztvf alltemp1.tar.gz 
drwxr-xr-x root/root         0 2022-04-23 01:45 ./0test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./1test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./2test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./3test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./4test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./5test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./6test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./7test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./8test/
drwxr-xr-x root/root         0 2022-04-23 01:45 ./9test/
-rw-r--r-- root/root        17 2022-04-22 05:25 ./alex.txt
-rw-r--r-- root/root     20480 2022-04-23 21:56 ./alltemp.tar
-rw-r--r-- root/root        14 2022-04-16 04:32 ./cesh1.txt
-rw-r--r-- root/root        39 2022-04-16 04:36 ./ceshi1.txt
-rw-r--r-- root/root         0 2022-04-16 05:31 ./ceshi.xt
-rw-r--r-- root/root        14 2022-04-19 21:34 ./mjj.txt
-rwxr-xr-x root/root        66 2022-04-16 04:14 ./test.sh
-rw-r--r-- root/root        43 2022-04-23 02:55 ./test.txt

5.单独的取出压缩文件中的内容

tar        -zxvf        alltemp.tar.gz        ./7test   #从alltemp.tar.gz中拷贝出7test文件

[root@timy-test-k8s01 Desktop]# tar -zxvf alltemp1.tar.gz ./7test
./7test/
[root@timy-test-k8s01 Desktop]# ls
7test  alltemp1.tar.gz

6.指定目录解压缩

[root@timy-test-k8s01 Desktop]# mkdir alltemp    #创建alltemp文件夹
[root@timy-test-k8s01 Desktop]# ls
7test  alltemp  alltemp1.tar.gz
[root@timy-test-k8s01 Desktop]# tar -zxvf alltemp1.tar.gz -C ./alltemp/
#将alltemp.tar.gz,指定解压到alltemp文件夹,-C指定解压路径
./0test/
./1test/
./2test/
./3test/
./4test/
./5test/
./6test/
./7test/
./8test/
./9test/
./alex.txt
./alltemp.tar
./cesh1.txt
./ceshi1.txt
./ceshi.xt
./mjj.txt
./test.sh
./test.txt

7.排除文件解压缩 --exclude

[root@timy-test-k8s01 Desktop]# tar -zxvf alltemp1.tar.gz #查看压缩文件内容
./0test/
./1test/
./2test/
./3test/
./4test/
./5test/
./6test/
./7test/
./8test/
./9test/
./alex.txt
./alltemp.tar
./cesh1.txt
./ceshi1.txt
./ceshi.xt
./mjj.txt
./test.sh
./test.txt
[root@timy-test-k8s01 Desktop]# tar -zvxf alltemp1.tar.gz --exclude alltemp.tar 
#除了alltemp.tar,其他的全部解压出来
./0test/
./1test/
./2test/
./3test/
./4test/
./5test/
./6test/
./7test/
./8test/
./9test/
./alex.txt
./cesh1.txt
./ceshi1.txt
./ceshi.xt
./mjj.txt
./test.sh
./test.txt

gzip命令

要说tar命令是个纸箱子用于打包,gzip命令就是压缩机器

gzip通过压缩算法lempel-ziv算法(lz77)将文件压缩为较小文件,节省60%以上的存储空间,以及网络传输速率

gzip无法压缩文件夹,必须先tar对文件夹打包后,才可以gzip压缩

gzip  (选项)    (参数)

 

-a或者--ascii:        使用ASCII文字模式;

-c或--stdout或--to-stdout        把解压后的文件输出到标准输出设备

-d或--decompress或----uncompress:        解开压缩文件;

-f或--force:强行压缩文件。不理会文件名称或硬链接是否存在以及该文件是否为符号连接

-h或--help:        在线帮助

-l或--list:列出压缩文件的相关信息

-L或--license:        显示版本与版权信息;

-n或--no-name:        压缩文件时,不保存原来的文件名称及时间戳记;

-N或--name:        压缩文件时,保存原来的文件名称及时间戳记;

-q或--quiet:        不显示警告信息

-r或--recursive:        递归处理,将指定目录下的所有文件及子目录一并处理;

-S或<压缩字尾字符串>或----suffix<压缩字尾字符串>:        更改压缩字尾字符串;

-t或--test:        测试压缩文件是否正确无误;

-v或--verbose:        显示指令执行过程;

-V或--version:        显示版本信息;

-<压缩效率>:压缩效率是一个介于1-9的数值,预设值为"6",指定愈大的数值,压缩效率就会愈高

--best:        此参数的效果和指定"-9"参数相同

--fast:        此参数的效果和指定"-1"参数相同

 案例

#压缩目录中每一个html文件为.gz,文件夹无法压缩,必须先tar打包

gzip *.html        #gzip压缩,解压都会删除源文件

 

1.压缩所有.txt文件

[root@timy-test-k8s01 alltemp]# gzip ./*.txt

2.列出压缩文件中信息

[root@timy-test-k8s01 alltemp]# gzip -l *.gz
         compressed        uncompressed  ratio uncompressed_name
                 46                  17 -11.8% alex.txt
                 44                  14 -14.3% cesh1.txt
                 66                  39   5.1% ceshi1.txt
                 42                  14 -14.3% mjj.txt
                 72                  43  -4.7% test.txt
                270                 127 -91.3% (totals)

zip命令

zip命令:是一个应用广泛的跨平台的压缩工具,压缩文件的后缀为zip文件,还可以压缩文件夹

 

语法:

zip        压缩文件名        要压缩的内容

 

-A        自动解压文件

-c        给压缩文件加注释

-d        删除文件

-F        修复损坏文件

-k        兼容DOS

-m        压缩完毕后,删除源文件

-q        运行时不显示信息处理信息

-r        处理指定目录和指定目录下的使用子目录

-v        显示信息的处理信息

-x        "文件列表"压缩时排除文件列表中的指定文件

-y        保留符号链接

-b<目录>        指定压缩到的目录

-i<格式>        匹配格式进行压缩

-L        显示版权信息

-t<日期>        指定压缩文件的日期

-<压缩率>        指定压缩率

案例

#压缩当前目录下所有内容为alltemp2.zip文件

[root@timy-test-k8s01 alltemp]# zip alltemp2.zip ./*

#压缩多个文件夹

[root@timy-test-k8s01 alltemp]# zip -r data.zip ./data ./data2

 unzip命令用于解压

参数

-l        显示压缩文件内所包含的文件

-d<目录>        指定文件解压缩后所要存储的目录

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值