Linux 压缩类命令 (tar gzip zip unzip compress uncompress zcat zdiff zless zgrep zipgrep)


这里是一段防爬虫文本,请读者忽略。
本文原创首发于CSDN,作者IDYS
博客首页:https://blog.csdn.net/weixin_41633902/
本文链接:https://blog.csdn.net/weixin_41633902/article/details/108799909
未经授权,禁止转载!恶意转载,后果自负!尊重原创,远离剽窃!


写在开头的话

  • 请记住:实践是掌握知识的最快方法
  • 如果你只是怀着看看的态度去快速浏览文章,而不去认认真真的把文章里面讲的任何一个知识点去实践一遍,那么你永远也掌握不了它
  • 生命不息,折腾不止!

Linux 压缩命令

00. tar 命令

0.1 tar 命令演示

  • tar命令说明:

    • 实现对多个文件或者多个目录的打包和压缩
  • 命令的语法格式

tar [OPTION] [FILE/DIRECTORY]

tar命令与后面的选项之间最少要有一个空格,同时tar带参数加-或者不加-都可以执行

  • tar命令的参数选项及其说明
参数选项含义
z通过gzip压缩或者解压
c创建新的tar
v显示详细过程
f指定压缩文件的名字
t不解压查看tar包的内容
p保持文件的原有属性
j通过bzip2命令压缩或解压
x解开tar
C指定解压的目录路径
--exclude=PATH打包时排除不需要处理的文件或者目录
-h打包软链接文件指向真实的源文件
--hard-dereference打包硬链接文件
-X 文件名从指定文件读取不需要处理的文件或者目录列表
-P以绝对路径打包
  • 创建gzip压缩文件
# 递归创建目录
[dayuanshuai@www tar_test] mkdir -p website/hexo/public/
# 批量创建文件
[dayuanshuai@www tar_test] touch website/hexo/public/html{1..10}.html

# 创建gzip 形式的压缩文件
[dayuanshuai@www tar_test] tar zcvf website.tar website/
website/
website/hexo/
website/hexo/public/
website/hexo/public/html1.html
website/hexo/public/html3.html
website/hexo/public/html4.html
website/hexo/public/html9.html
website/hexo/public/html8.html
website/hexo/public/html6.html
website/hexo/public/html10.html
website/hexo/public/html5.html
website/hexo/public/html2.html
website/hexo/public/html7.html

# 查看文件大小
[dayuanshuai@www tar_test]  ll -h
total 8.0K
drwxrwxr-x. 3 dayuanshuai dayuanshuai 4.0K Sep  6 10:39 website
-rw-rw-r--. 1 dayuanshuai dayuanshuai  268 Sep  6 10:40 website.tar
  • 不解压查看压缩文件
# 当使用 t 不解压查看文件时。 加上 v 选项可以查看 文件的详细属性信息
[dayuanshuai@www tar_test] tar -ztvf website.tar 
drwxrwxr-x dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/
drwxrwxr-x dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/
drwxrwxr-x dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html1.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html3.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html4.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html9.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html8.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html6.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html10.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html5.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html2.html
-rw-rw-r-- dayuanshuai/dayuanshuai 0 2019-09-06 10:39 website/hexo/public/html7.html

# 之查看压缩包的文件,但是不显示详细的文件属性信息
[dayuanshuai@www tar_test] tar -ztf website.tar  
website/
website/hexo/
website/hexo/public/
website/hexo/public/html1.html
website/hexo/public/html3.html
website/hexo/public/html4.html
website/hexo/public/html9.html
website/hexo/public/html8.html
website/hexo/public/html6.html
website/hexo/public/html10.html
website/hexo/public/html5.html
website/hexo/public/html2.html
website/hexo/public/html7.html

# 当不指定文件的压缩格式时,tar也会自动判断文件的压缩类型
[dayuanshuai@www tar_test] tar -tf website.tar.gz 
website/
website/hexo/
website/hexo/public/
website/hexo/public/html1.html
website/hexo/public/html3.html
website/hexo/public/html4.html
website/hexo/public/html9.html
website/hexo/public/html8.html
website/hexo/public/html6.html
website/hexo/public/html10.html
website/hexo/public/html5.html
website/hexo/public/html2.html
website/hexo/public/html7.html
  • 解压文件到指定目录
# 使用 -C 选项指定解压目录 v 选项 显示解压的详细情况 x选项:解压
[dayuanshuai@www tar_test] tar zxvf website.tar.gz -C test/onetest/
website/
website/hexo/
website/hexo/public/
website/hexo/public/html1.html
website/hexo/public/html3.html
website/hexo/public/html4.html
website/hexo/public/html9.html
website/hexo/public/html8.html
website/hexo/public/html6.html
website/hexo/public/html10.html
website/hexo/public/html5.html
website/hexo/public/html2.html
website/hexo/public/html7.html
[dayuanshuai@www tar_test] tree test/
test/
└── onetest
    └── website
        └── hexo
            └── public
                ├── html10.html
                ├── html1.html
                ├── html2.html
                ├── html3.html
                ├── html4.html
                ├── html5.html
                ├── html6.html
                ├── html7.html
                ├── html8.html
                └── html9.html

4 directories, 10 files

# 创建目录
[dayuanshuai@www tar_test] mkdir new_test

# 解压文件到指定目录, 解压文件时 不使用 z 指定 解压类型也可以。tar会自动判断文件的解压类型,进行自动解压。 
[dayuanshuai@www tar_test] tar xfC website.tar.gz new_test/
[dayuanshuai@www tar_test] tree new_test/
new_test/
└── website
    └── hexo
        └── public
            ├── html10.html
            ├── html1.html
            ├── html2.html
            ├── html3.html
            ├── html4.html
            ├── html5.html
            ├── html6.html
            ├── html7.html
            ├── html8.html
            └── html9.html

3 directories, 10 files

# 打包压缩 website 目录。 但是把 website/hexo 目录排除在外, 注意排除的目录结尾主要加上 / 否则不会成功
[dayuanshuai@www tar_test] tar jcvf sh.tar.bz2 website --exclude=website/hexo
website/
website/wordpree/
website/wordpree/www/
website/wordpree/www/sh3.sh
website/wordpree/www/sh5.sh
website/wordpree/www/sh4.sh
website/wordpree/www/sh6.sh
website/wordpree/www/sh7.sh
website/wordpree/www/sh1.sh
website/wordpree/www/sh8.sh
website/wordpree/www/sh10.sh
website/wordpree/www/sh9.sh
website/wordpree/www/sh2.sh

# 打包 website 目录,排除掉 website/hexo/public/html1.html website/hexo/public/html2.html website/hexo/public/html3.html 三个文件
[dayuanshuai@www tar_test] tar jcvf sh.tar.bz2 website --exclude=website/hexo/public/html{1..3}.html
website/
website/hexo/
website/hexo/public/
website/hexo/public/html4.html
website/hexo/public/html9.html
website/hexo/public/html8.html
website/hexo/public/html6.html
website/hexo/public/html10.html
website/hexo/public/html5.html
website/hexo/public/html7.html
website/wordpree/
website/wordpree/www/
website/wordpree/www/sh3.sh
website/wordpree/www/sh5.sh
website/wordpree/www/sh4.sh
website/wordpree/www/sh6.sh
website/wordpree/www/sh7.sh
website/wordpree/www/sh1.sh
website/wordpree/www/sh8.sh
website/wordpree/www/sh10.sh
website/wordpree/www/sh9.sh
website/wordpree/www/sh2.sh

# --excluded 可以重复使用
[dayuanshuai@www tar_test] tar jcvf sh.tar.bz2 website --exclude=website/hexo/public/html{1..3}.html --exclude=website/hexo/public/html{8,9}.html
website/
website/hexo/
website/hexo/public/
website/hexo/public/html4.html
website/hexo/public/html6.html
website/hexo/public/html10.html
website/hexo/public/html5.html
website/hexo/public/html7.html
website/wordpree/
website/wordpree/www/
website/wordpree/www/sh3.sh
website/wordpree/www/sh5.sh
website/wordpree/www/sh4.sh
website/wordpree/www/sh6.sh
website/wordpree/www/sh7.sh
website/wordpree/www/sh1.sh
website/wordpree/www/sh8.sh
website/wordpree/www/sh10.sh
website/wordpree/www/sh9.sh
website/wordpree/www/sh2.sh
  • 从指定文件读取需要排除的文件或者目录列表
[dayuanshuai@www tar_test] tree website | grep --color -E 'html[0-7]{1}[^[:digit:]]+' | awk -F ' ' '{printf $3"\n"}'
html1.html
html2.html
html3.html
html4.html
html5.html
html6.html
html7.html
# 文件追加至需要 指定 排除的文件上
[dayuanshuai@www tar_test] tree website | grep --color -E 'html[0-7]{1}[^[:digit:]]+' | awk -F ' ' '{printf $3"\n"}' > remove_file.txt1

# 再追加一个目录上去
[dayuanshuai@www tar_test] echo website/wordpree/www >> remove_file.txt 

# 查看文件内容
[dayuanshuai@www tar_test] cat remove_file.txt 
html1.html
html2.html
html3.html
html4.html
html5.html
html6.html
html7.html
website/wordpree/www

# 使用 X 指定 需要排除的文件  注意文件格式为 tar 选项 生成的压缩文件  读取的排除文件 目录
[dayuanshuai@www tar_test] tar jcvfX exclude_file.tar.bz2 remove_file.txt website
website/
website/hexo/
website/hexo/public/
website/hexo/public/html9.html
website/hexo/public/html8.html
website/hexo/public/html10.html
website/wordpree/

# 可以看到 排除了  website/wordpress/ 底下的 www 目录 同时也排除了 html1.html 到 html7.html 文件

  • 打包链接文件对应的压缩文件
# -P 以绝对路径 打包压缩文件
[root@www ~] tar jcvPf selinux.link.tar.bz2 /etc/sysconfig/selinux    
/etc/sysconfig/selinux

# 查看压缩文件, 发现其指示一个 链接文件。 并不是实际文件
[root@www ~] tar jtvPf selinux.link.tar.bz2 
lrwxrwxrwx root/root         0 2019-08-21 02:30 /etc/sysconfig/selinux -> ../selinux/config

# 可以查看要打包的文件是一个链接文件
[root@www etc] ls -l sysconfig/selinux 
lrwxrwxrwx. 1 root root 17 Aug 21 02:30 sysconfig/selinux -> ../selinux/config

# 加 -h 参数 打包链接文件 指向的真实文件 
[root@www etc] tar jcvhf selinux.hardlink.tar.bz2 sysconfig/selinux 
sysconfig/selinux

# 查看压缩包 详情
[root@www etc] tar tvf selinux.hardlink.tar.bz2 
-rw-r--r-- root/root       458 2019-08-21 02:30 sysconfig/selinux

# -C解压压缩包到指定目录
[root@www etc] tar -xvf selinux.hardlink.tar.bz2 -C ~/data/
sysconfig/selinux

# 查看文件内容。可以看到 这次打包打包的是链接文件指向的真实文件
[root@www etc] cat ~/data/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

0.2 tar 命令注意事项

  • 关于--exclude

    • 若需要打包的目录为相对路径,则--exclude后只能接相对路径
    • 若需要打包的目录为绝对路径,则--exclude后既能接绝对路径又能接相对路径
    • 为了方便起见,--exclude后接路径和打包路径应该保持一致,要么都是相对路径。要么都是绝对路径
  • 关于-P参数的危险性说明

# 在 tmp 目录下创建 一个文件, 文件内容为 test.txt
[root@www ~] echo test1 > /tmp/test.txt
[root@www ~] cat /tmp/test.txt 
test1

# 以绝对路径打包
[root@www ~] tar jcfP test.tar.bz2 /tmp/test.txt

# 往文件里面追加内容 test2, 此时/tmp/test.txt 的内容已经发生改变
[root@www ~] echo test2 >> /tmp/test.txt     
[root@www ~] cat /tmp/test.txt 
test1
test2

# 指定 解压路径
[root@www ~] tar xvf test.tar.bz2 -C data/
tar: Removing leading `/' from member names
/tmp/test.txt

# 此时查看 原来的 /tmp/test.txt 文件发现内容没有影响
 [root@www ~] cat /tmp/test.txt 
test1
test2

# 直接解压缩, tar 自动移除了 /tmp/test.txt 前面的  / , 所以文件解压还是以 相对路径解压
[root@www ~] tar xvf test.tar.bz2
tar: Removing leading `/' from member names
/tmp/test.txt
# 文件内容没有改变
[root@www ~] cat /tmp/test.txt 
test1
test2

# 此时加上 -P 参数以 绝对路径解压。 这个时候小心了。 以绝对路径解压后会直接覆盖同名的文件
[root@www ~] tar xvfP test.tar.bz2
/tmp/test.txt
[root@www ~] cat /tmp/test.txt 
test1

-P 选项,以绝对路径解压缩或压缩。 注意:千万不要以-P选项解压缩,这样的话会以绝对路径解压缩。这可能会导致你的很多文件直接被覆盖。所以-P选项,慎用!慎用!

  • 打包/etc/sysconfig下所有的链接文件对应的原文件
# 查看sysconfig 目录下的压缩文件
[root@www ~] find /etc/sysconfig -type l -exec ls -lh {} \;  
lrwxrwxrwx. 1 root root 17 Aug 21 02:30 /etc/sysconfig/selinux -> ../selinux/config
lrwxrwxrwx. 1 root root 18 Aug 21 02:30 /etc/sysconfig/network-scripts/ifup -> ../../../sbin/ifup
lrwxrwxrwx. 1 root root 9 Aug 21 02:30 /etc/sysconfig/network-scripts/ifup-isdn -> ifup-ippp
lrwxrwxrwx. 1 root root 11 Aug 21 02:30 /etc/sysconfig/network-scripts/ifdown-isdn -> ifdown-ippp
lrwxrwxrwx. 1 root root 20 Aug 21 02:30 /etc/sysconfig/network-scripts/ifdown -> ../../../sbin/ifdown

# 打包 链接文件
[root@www ~] tar -jcvf sysconfig_filelink.bz2 `find /etc/sysconfig -type l`        
tar: Removing leading `/' from member names
/etc/sysconfig/selinux
/etc/sysconfig/network-scripts/ifup
/etc/sysconfig/network-scripts/ifup-isdn
/etc/sysconfig/network-scripts/ifdown-isdn
/etc/sysconfig/network-scripts/ifdown

# 加上 -v查看 详情 发现 打包的全是链接文件
[root@www ~] tar -jvtf sysconfig_filelink.bz2 
lrwxrwxrwx root/root         0 2019-08-21 02:30 etc/sysconfig/selinux -> ../selinux/config
lrwxrwxrwx root/root         0 2019-08-21 02:30 etc/sysconfig/network-scripts/ifup -> ../../../sbin/ifup
lrwxrwxrwx root/root         0 2019-08-21 02:30 etc/sysconfig/network-scripts/ifup-isdn -> ifup-ippp
lrwxrwxrwx root/root         0 2019-08-21 02:30 etc/sysconfig/network-scripts/ifdown-isdn -> ifdown-ippp
lrwxrwxrwx root/root         0 2019-08-21 02:30 etc/sysconfig/network-scripts/ifdown -> ../../../sbin/ifdown

# 打包链接文件对应的原文件
[root@www ~] tar -jcvhf sysconfig_filel_hardlink.bz2 `find /etc/sysconfig -type l`  
tar: Removing leading `/' from member names
/etc/sysconfig/selinux
/etc/sysconfig/network-scripts/ifup
/etc/sysconfig/network-scripts/ifup-isdn
/etc/sysconfig/network-scripts/ifdown-isdn
/etc/sysconfig/network-scripts/ifdown
[root@www ~] tar tvf sysconfig_filel_hardlink.bz2 
-rw-r--r-- root/root       458 2019-08-21 02:30 etc/sysconfig/selinux
-rwxr-xr-x root/root      4367 2013-10-10 22:48 etc/sysconfig/network-scripts/ifup
-rwxr-xr-x root/root     11971 2013-10-10 22:48 etc/sysconfig/network-scripts/ifup-isdn
-rwxr-xr-x root/root       781 2013-10-10 22:48 etc/sysconfig/network-scripts/ifdown-isdn
-rwxr-xr-x root/root      1510 2013-10-10 22:48 etc/sysconfig/network-scripts/ifdown

01. gzip 压缩或者解压缩

1.1 命令说明

  • gzip功能说明

    • gzip命令用于将一个大的文件通过压缩算法变成一个小的文件。gzip命令不能直接压缩目录
  • gzip语法格式

gzip [option] [file]
  • 选项说明
参数选项解释说明
-d解开压缩文件
-v显示指令执行的过程
-l列出压缩文件的内容信息
-c将内容输出到标准输出,不改变原始文件
-f对目录下的所有文件递归进行压缩操作
- 数字<1 - 9 >指定压缩率,默认为6,值越大压缩率越高
-t测试,检查压缩文件是否完整
  • gzip的所有操作都是对单个文件执行的

1.2 使用演示

  • 批量压缩文件
# 创建 9个压缩文件
[dayuanshuai@www html_test] touch index{1..9}.css
[dayuanshuai@www html_test] ls
index1.css  index2.css  index3.css  index4.css  index5.css  index6.css  index7.css  index8.css  index9.css
# gzip 压缩所有以.css结尾的文件。这里采用 通配符机制
[dayuanshuai@www html_test] gzip *.css
[dayuanshuai@www html_test] ls
index1.css.gz  index3.css.gz  index5.css.gz  index7.css.gz  index9.css.gz
index2.css.gz  index4.css.gz  index6.css.gz  index8.css.gz
# gzip -d 解压缩
[dayuanshuai@www html_test] gzip -d *.gz
[dayuanshuai@www html_test] ls
index1.css  index2.css  index3.css  index4.css  index5.css  index6.css  index7.css  index8.css  index9.css
  • 为每一个文件添加内容后进行压缩。压缩后查看其详细信息
# 为每一个文件添加内容
[dayuanshuai@www html_test] for i in `ls ./`;do echo 'miss you' > $i; done                   
# 查看所有文件的内容
[dayuanshuai@www html_test] cat *.css
miss you
miss you
miss you
miss you
miss you
miss you
miss you
miss you
miss you
# 查看压缩信息
[dayuanshuai@www html_test] gzip -l *.gz
         compressed        uncompressed  ratio uncompressed_name
                 40                   9  66.7% index1.css
                 40                   9  66.7% index2.css
                 40                   9  66.7% index3.css
                 40                   9  66.7% index4.css
                 40                   9  66.7% index5.css
                 40                   9  66.7% index6.css
                 40                   9  66.7% index7.css
                 40                   9  66.7% index8.css
                 40                   9  66.7% index9.css
                360                  81 -298.8% (totals)
  • 解压文件,同时查看详细的解压过程
[dayuanshuai@www html_test]  gzip -dv *.gz
index1.css.gz:   66.7% -- replaced with index1.css
index2.css.gz:   66.7% -- replaced with index2.css
index3.css.gz:   66.7% -- replaced with index3.css
index4.css.gz:   66.7% -- replaced with index4.css
index5.css.gz:   66.7% -- replaced with index5.css
index6.css.gz:   66.7% -- replaced with index6.css
index7.css.gz:   66.7% -- replaced with index7.css
index8.css.gz:   66.7% -- replaced with index8.css
index9.css.gz:   66.7% -- replaced with index9.css
# 这个时候查看,发现压缩文件都消失了,只留下 解压缩文件
[dayuanshuai@www html_test] ls
index1.css  index2.css  index3.css  index4.css  index5.css  index6.css  index7.css  index8.css  index9.css

使用gzip压缩或者是解压缩都不会保留原文件

  • 如果使用gzip的同时保留原来的文件呢。下面看我的演示
# 压缩文件,并且将压缩后的内容 输出至终端, 这里我直接重定向到新的文件
[dayuanshuai@www html_test] gzip -c index7.css > index7.css.gz

# 解压文件, 将解压后的内容重定向到某个文件
[dayuanshuai@www html_test] gzip -dc index7.css.gz > index7_1.css 

[dayuanshuai@www html_test] cat index7.css index7_1.css            
miss you
miss you

# 查看比对两个文件,发现他们的内容是一样的
[dayuanshuai@www html_test] diff index7.css index7_1.css 
  • 创建一个文件奇数行为me偶数行为hello world
[dayuanshuai@www html_test] for i in `seq 1 100`;do if [ $(($i % 2 )) -eq 0  ];then echo "hello world" >> index8.css;else echo "me" >> index8.css;fi;done 
[dayuanshuai@www html_test] cat index8.css | wc -l
100

# 再为 index9.css 创建同样的文件,不过第一行已经有一个 miss you 内容了,所以需要删除
[dayuanshuai@www html_test] for i in `seq 1 100`;do if [ $(($i % 2 )) -eq 0  ];then echo "hello world" >> index9.css;else echo "me" >> index9.css;fi;done 

# 删除第一行文件,同时为文件做备份
[dayuanshuai@www html_test] sed -i.bak '1d' index9.css

# 比较两个文件,发现完全相同
[dayuanshuai@www html_test] diff index8.css index9.css
  • gzip套件里面有很多可以在不解压文件的情况下处理压缩文件里面的原内容的程序,如zcat、 zgrep、 zless、zdiff,他们的作用和cat 、grep、less、diff相同,但是他们操作的是压缩文件
# 查看压缩文件内容
[dayuanshuai@www html_test]  zcat index8.css.gz 
me
hello world
me
hello world
me
hello world
me
hello world
me
hello world
me
hello world
me
.............. # 后面省略

# 筛选压缩文件内容
[dayuanshuai@www html_test] zgrep --color -E llo index8.css.gz       
hello world
hello world
hello world
hello world
hello world
hello world
hello world

# 比较两个压缩文件内容
[dayuanshuai@www html_test] zdiff index8.css.gz index9.css.gz 

02. zip 打包和压缩文件

2.1 命令说明

  • 功能说明
    • zip压缩格式是WindowsLinux等多个平台通用的压缩格式。和zip命令相比,zip命令压缩文件不仅不会删除源文件,而且还能压缩目录
  • zip命令语法格式
zip [option] [file]
  • zip命令常用选项说明
参数选项解释说明
-r将指定目录下的所有文件和子目录一并压缩
-x压缩文件时排除某个文件
-q不显示压缩信息
-d添加某个文件到压缩文件
-v显示执行过程

2.2 命令演示

  • 压缩文件
# 复制文件到 到当前目录下
[dayuanshuai@www zip_tar] cp /etc/fstab ./
[dayuanshuai@www zip_tar] ls
fstab
[dayuanshuai@www zip_tar] pwd
/home/dayuanshuai/compre_test/zip_test/zip_tar
[dayuanshuai@www zip_tar] ls
fstab
# 压缩fstab 文件,同时指定压缩名称
[dayuanshuai@www zip_tar] zip fstab.zip fstab 
  adding: fstab (deflated 61%)
# -q 不显示压缩信息
[dayuanshuai@www zip_tar] zip -q fstab1.zip fstab 
[dayuanshuai@www zip_tar] ls
fstab  fstab1.zip  fstab.zip

# 压缩文件时 排除 zip_direc/services 这个文件
[dayuanshuai@www zip_tar] zip -rv temp1.zip zip_direc/ -x zip_direc/services 
  adding: zip_direc/    (in=0) (out=0) (stored 0%)
  adding: zip_direc/fstab       (in=993) (out=389) (deflated 61%)
total bytes=993, compressed=389 -> 61% savings

# 添加fstab文件到 myzip.zip 文件里面
[dayuanshuai@www zip_tar] zip -dv myzip.zip fstab
1>1:   adding: fstab (deflated 61%)

03. unzip:解压zip文件

3.1 命令说明

  • 功能说明

    • unzip命令可以解压zip命令或其他压缩软件压缩的zip格式的文件
  • 语法格式

unzip [option] [file]
  • unzip选项说明
参数选项含义
-l不解压显示压缩包内容
-d指定解压目录
-o解压时不提示是否覆盖文件
-v显示zip压缩包里面文件的详细信息

3.2 命令演示

  • 显示压缩文件里面的详细信息
[dayuanshuai@www zip_tar] unzip -l myzip.zip 
Archive:  myzip.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  09-07-2019 02:36   zip_direc/
      993  09-07-2019 02:36   zip_direc/fstab
   641020  09-07-2019 02:36   zip_direc/services
      993  09-07-2019 02:31   fstab
---------                     -------
   643006                     4 files

# 往 添加myzip.zip 压缩包里面添加 文件 
[dayuanshuai@www zip_tar] zip -dv myzip.zip inittab           
1>1:   adding: inittab (deflated 47%)
[dayuanshuai@www zip_tar] unzip -l myzip.zip 
Archive:  myzip.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  09-07-2019 02:36   zip_direc/
      993  09-07-2019 02:36   zip_direc/fstab
   641020  09-07-2019 02:36   zip_direc/services
      993  09-07-2019 02:31   fstab
      884  09-07-2019 03:09   inittab
---------                     -------
   643890                     5 file
  • 查看压缩包里面的详细内容
# 递归创建目录
[dayuanshuai@www zip_tar] mkdir -p who/am/i
# 复制 压缩文件到指定目录下
[dayuanshuai@www zip_tar] cp fstab.zip who/am/i/
# 进入到目录下
[dayuanshuai@www zip_tar] cd who/am/i/
# 复制压缩包到该目录下
[dayuanshuai@www i] cp ../../../myzip.zip ./
[dayuanshuai@www i] ls
fstab.zip  myzip.zip

# 解压文件,同时显示详细的解压过程
[dayuanshuai@www i] unzip myzip.zip 
Archive:  myzip.zip
   creating: zip_direc/
  inflating: zip_direc/fstab         
  inflating: zip_direc/services      
  inflating: fstab                   
  inflating: inittab      

# 查看改目录下的文件
[dayuanshuai@www i] ls
fstab  fstab.zip  inittab  myzip.zip  zip_direc

# 删除除压缩包 以外的其他文件或者目录
[dayuanshuai@www i] ls | grep -v .zip | xargs -n1 -I {} rm -rf {}  

# 查看压缩包详细信息
[dayuanshuai@www i] unzip -v fstab.zip 
Archive:  fstab.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
     993  Defl:N      389  61% 09-07-2019 02:31 f3497c12  fstab
--------          -------  ---                            -------
     993              389  61%                            1 file
     

04. compress 和 uncompress 信息

4.1 命令说明

  • compressuncompress

  • compress

    • -r:递归压缩目录下的所有文件
    • -d:解压缩
    • -f:强迫写入档案,若目的档已经存在,则会被覆盖 (force)
    • -c:输出结果至标准输出设备(一般指荧幕)
    • -v:将程序执行的讯息印在荧幕上 verbose
    • -b:设定压缩比例,可以设定的值为9-16

4.2 命令演示

# 递归创建目录
[dayuanshuai@node3 ~] mkdir -p a/b/c/d
# 创建多个文件
[dayuanshuai@node3 ~] touch a/b/{a,b,c/c,c/d/who}.txt
# 查看目录状态
[dayuanshuai@node3 ~] tree a
a
└── b
    ├── a.txt
    ├── b.txt
    └── c
        ├── c.txt
        └── d
            └── who.txt

3 directories, 4 files

# 递归压缩目录下所有的文件
[dayuanshuai@node3 ~] compress -rf a
# 查看a 目录下的文件, 发现所有的文件都已经被压缩为.Z的格式
[dayuanshuai@node3 ~] tree a
a
└── b
    ├── a.txt.Z
    ├── b.txt.Z
    └── c
        ├── c.txt.Z
        └── d
            └── who.txt.Z

3 directories, 4 files

# 递归解压缩 a 目录下的文件
[dayuanshuai@node3 ~] compress -rd a
# 现在 a 下面所有的文件均被解压
[dayuanshuai@node3 ~] tree a
a
└── b
    ├── a.txt
    ├── b.txt
    └── c
        ├── c.txt
        └── d
            └── who.txt

3 directories, 4 files


  • uncompress
    • -f:不提示用户,强制覆盖掉目标文件
    • -c:将结果送到标准输出,无文件被改变
    • -r:递归的操作方式
[dayuanshuai@node3 ~] compress -rf a
[dayuanshuai@node3 ~] tree a
a
└── b
    ├── a.txt.Z
    ├── b.txt.Z
    └── c
        ├── c.txt.Z
        └── d
            └── who.txt.Z

# 批量解压缩
[dayuanshuai@node3 ~] uncompress -rfv a
a/b/c/d/who.txt.Z:  -- replaced with a/b/c/d/who.txt
a/b/c/c.txt.Z:  -- replaced with a/b/c/c.txt
a/b/a.txt.Z:  -- replaced with a/b/a.txt
a/b/b.txt.Z:  -- replaced with a/b/b.txt
[dayuanshuai@node3 ~] tree a
a
└── b
    ├── a.txt
    ├── b.txt
    └── c
        ├── c.txt
        └── d
            └── who.txt

3 directories, 4 files


写在最后的话:

  • 无论每个知识点的难易程度如何,我都会尽力将它描绘得足够细致
  • 欢迎关注我的CSDN博客,IDYS’BLOG
  • 持续更新内容
    linux基础 | 数据通信(路由交换,WLAN) | Python基础 | 云计算
  • 如果你有什么疑问,或者是难题。欢迎评论或者私信我。你若留言,我必回复!
  • 虽然我现在还很渺小,但我会做好每一篇内容。谢谢关注!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值