准备工作

[root@centos-01 ~]# cd /tmp/
[root@centos-01 tmp]# ls
1.txt
systemd-private-40c0e692674844949b91361dc6ab4a40-chronyd.service-6km7K9
systemd-private-40c0e692674844949b91361dc6ab4a40-vgauthd.service-3W1kyg
systemd-private-40c0e692674844949b91361dc6ab4a40-vmtoolsd.service-wJZPyD
[root@centos-01 tmp]# mkdir mytest
[root@centos-01 tmp]# cd mytest/
[root@centos-01 mytest]# ls
[root@centos-01 mytest]# find /etc/ -type f -name "*conf"
/etc/lvm/lvm.conf
/etc/lvm/lvmlocal.conf
/etc/resolv.conf
/etc/pki/ca-trust/ca-legacy.conf
/etc/yum/pluginconf.d/fastestmirror.conf
/etc/yum/pluginconf.d/langpacks.conf
/etc/yum/protected.d/systemd.conf
/etc/yum/version-groups.conf
/etc/asound.conf
/etc/sasl2/smtpd.conf
/etc/logrotate.conf
/etc/openldap/ldap.conf
/etc/libuser.conf
/etc/sestatus.conf
/etc/yum.conf
/etc/host.conf
/etc/dracut.conf
/etc/dbus-1/system.d/teamd.conf
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf
/etc/dbus-1/system.d/org.freedesktop.import1.conf
/etc/dbus-1/system.d/org.freedesktop.locale1.conf
/etc/dbus-1/system.d/org.freedesktop.login1.conf
/etc/dbus-1/system.d/org.freedesktop.machine1.conf
/etc/dbus-1/system.d/org.freedesktop.systemd1.conf
/etc/dbus-1/system.d/org.freedesktop.timedate1.conf
/etc/dbus-1/system.d/org.freedesktop.PolicyKit1.conf
/etc/dbus-1/system.d/wpa_supplicant.conf
/etc/dbus-1/system.d/nm-dispatcher.conf
/etc/dbus-1/system.d/nm-ifcfg-rh.conf
/etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf
/etc/dbus-1/system.d/com.redhat.tuned.conf
/etc/dbus-1/system.d/FirewallD.conf
/etc/dbus-1/system.d/dnsmasq.conf
/etc/dbus-1/session.conf
/etc/dbus-1/system.conf
/etc/depmod.d/dist.conf
/etc/modprobe.d/tuned.conf
/etc/systemd/bootchart.conf
/etc/systemd/coredump.conf
/etc/systemd/journald.conf
/etc/systemd/logind.conf
/etc/systemd/system.conf
/etc/systemd/user.conf
/etc/rsyslog.d/listen.conf
/etc/X11/xorg.conf.d/00-keyboard.conf
/etc/prelink.conf.d/nss-softokn-prelink.conf
/etc/prelink.conf.d/fipscheck.conf
/etc/prelink.conf.d/grub2.conf
/etc/ld.so.conf
/etc/ld.so.conf.d/mariadb-x86_64.conf
/etc/ld.so.conf.d/kernel-3.10.0-693.el7.x86_64.conf
/etc/nsswitch.conf
/etc/udev/udev.conf
/etc/NetworkManager/NetworkManager.conf
/etc/libaudit.conf
/etc/fuse.conf
/etc/GeoIP.conf
/etc/selinux/semanage.conf
/etc/selinux/targeted/setrans.conf
/etc/sysctl.conf
/etc/krb5.conf
/etc/security/pwquality.conf
/etc/security/access.conf
/etc/security/chroot.conf
/etc/security/group.conf
/etc/security/limits.conf
/etc/security/limits.d/20-nproc.conf
/etc/security/namespace.conf
/etc/security/pam_env.conf
/etc/security/sepermit.conf
/etc/security/time.conf
/etc/wpa_supplicant/wpa_supplicant.conf
/etc/plymouth/plymouthd.conf
/etc/tuned/tuned-main.conf
/etc/vmware-tools/guestproxy-ssl.conf
/etc/vmware-tools/vgauth.conf
/etc/firewalld/firewalld.conf
/etc/kdump.conf
/etc/audisp/audispd.conf
/etc/audisp/plugins.d/af_unix.conf
/etc/audisp/plugins.d/syslog.conf
/etc/audit/auditd.conf
/etc/chrony.conf
/etc/rsyslog.conf
/etc/man_db.conf
/etc/sudo-ldap.conf
/etc/sudo.conf
/etc/e2fsck.conf
/etc/mke2fs.conf
/etc/vconsole.conf
/etc/locale.conf
/etc/updatedb.conf
/etc/dnsmasq.conf
[root@centos-01 mytest]# find /etc/ -type f -name "*conf" -exec cat {} >> 2.txt \;
[root@centos-01 mytest]# ls
2.txt
[root@centos-01 mytest]# du -sh 2.txt 
240K    2.txt
[root@centos-01 mytest]# find /etc/ -type f -name "*conf" -exec cat {} >> 2.txt \;
[root@centos-01 mytest]# du -sh 2.txt 
476K    2.txt
[root@centos-01 mytest]# find /etc/ -type f -name "*conf" -exec cat {} >> 2.txt \;
[root@centos-01 mytest]# du -sh 2.txt 
1.2M    2.txt
[root@centos-01 mytest]# find /etc/ -type f -name "*conf" -exec cat {} >> 2.txt \;
[root@centos-01 mytest]# du -sh 2.txt 
1.2M    2.txt
[root@centos-01 mytest]# find /etc/ -type f -name "*conf" -exec cat {} >> 2.txt \;
[root@centos-01 mytest]# du -sh 2.txt 
1.2M    2.txt
[root@centos-01 mytest]# find /etc/ -type f -name "*conf" -exec cat {} >> 2.txt \;
[root@centos-01 mytest]# du -sh 2.txt 
2.2M    2.txt
[root@centos-01 mytest]# wc -l 2.txt 
36486 2.txt

gzip压缩工具

语法:gzip [-dn] filename(n为1-9的数字)

  • “-d”:解压缩时使用
  • “-n” (n为1-9的数字):压缩等级,第1级为最快压缩,但压缩量最小;第9级的压缩速度最慢,但压缩量最大。默认级别为6,“--fast”和“--best”分别等价于“-1”和“-9”。
[root@centos-01 mytest]# gzip 2.txt 
[root@centos-01 mytest]# ls
2.txt.gz
[root@centos-01 mytest]# du -sh 2.txt.gz 
376K    2.txt.gz
[root@centos-01 mytest]# gzip -d 2.txt.gz 
[root@centos-01 mytest]# ls
2.txt
[root@centos-01 mytest]# du -sh 2.txt 
1.4M    2.txt
[root@centos-01 mytest]# wc -l 2.txt 
36486 2.txt
[root@centos-01 mytest]# gzip -1 2.txt 
[root@centos-01 mytest]# du -sh 2.txt.gz 
444K    2.txt.gz
[root@centos-01 mytest]# gunzip 2.txt.gz 
[root@centos-01 mytest]# ls
2.txt
[root@centos-01 mytest]# gzip -9 2.txt 
[root@centos-01 mytest]# du -sh 2.txt.gz 
372K    2.txt.gz
[root@centos-01 mytest]# ls
2.txt.gz
[root@centos-01 mytest]# file 2.txt.gz 
2.txt.gz: gzip compressed data, was "2.txt", from Unix, last modified: Fri May  4 10:49:46 2018, max compression
  • zcat命令查看压缩文件内容
[root@centos-01 mytest]# zcat 2.txt.gz

[root@centos-01 mytest]# ls
2.txt.gz
[root@centos-01 mytest]# gzip -d 2.txt.gz 
[root@centos-01 mytest]# gzip -c 2.txt > /tmp/2.txt.gz
[root@centos-01 mytest]# ls
2.txt
[root@centos-01 mytest]# ls /tmp/
1.txt     systemd-private-ad61a62cb5b4492ca6ac55582093dd32-chronyd.service-yzD92d
2.txt.gz  systemd-private-ad61a62cb5b4492ca6ac55582093dd32-vgauthd.service-inJewP
mytest    systemd-private-ad61a62cb5b4492ca6ac55582093dd32-vmtoolsd.service-Exaldt
[root@centos-01 mytest]# file /tmp/2.txt.gz 
/tmp/2.txt.gz: gzip compressed data, was "2.txt", from Unix, last modified: Fri May  4 10:49:46 2018
[root@centos-01 mytest]# gzip -d -c /tmp/2.txt.gz > /tmp/mytest/3.txt
[root@centos-01 mytest]# ls
2.txt  3.txt
[root@centos-01 mytest]# wc -l 2.txt 3.txt 
  36486 2.txt
  36486 3.txt
  72972 总用量
[root@centos-01 mytest]# du -sh *.txt
1.4M    2.txt
1.4M    3.txt
  • gzip是不支持压缩目录的。

bzip2压缩工具

[root@centos-01 mytest]# bzip2 2.txt 
-bash: bzip2: 未找到命令
[root@centos-01 mytest]# yum install -y bzip2

语法:bzip2 [-dz] filename

  • “-d”:解压缩
  • “-z”:压缩
[root@centos-01 mytest]# bzip2 2.txt 
[root@centos-01 mytest]# ls
2.txt.bz2  3.txt
[root@centos-01 mytest]# du -sh 2.txt.bz2 
160K    2.txt.bz2
[root@centos-01 mytest]# bzip2 -d 2.txt.bz2 
[root@centos-01 mytest]# ls
2.txt  3.txt
[root@centos-01 mytest]# bzip2 2.txt 
[root@centos-01 mytest]# bunzip2 2.txt.bz2 
[root@centos-01 mytest]# ls
2.txt  3.txt
[root@centos-01 mytest]# ls /tmp/
1.txt     systemd-private-604486059e93474a8b695802ff294d42-chronyd.service-nvD2qs
2.txt.gz  systemd-private-604486059e93474a8b695802ff294d42-vgauthd.service-xKKbjw
mytest    systemd-private-604486059e93474a8b695802ff294d42-vmtoolsd.service-yaJKgA
[root@centos-01 mytest]# bzip2 -c 2.txt > /tmp/3.txt.bz2
[root@centos-01 mytest]# du -sh /tmp/3.txt.bz2 
160K    /tmp/3.txt.bz2
[root@centos-01 mytest]# bzip2 -d -c /tmp/3.txt.bz2 > 4.txt
[root@centos-01 mytest]# ls
2.txt  3.txt  4.txt
[root@centos-01 mytest]# du -sh 4.txt 
1.4M    4.txt
  • bzcat命令查看压缩文件内容
[root@centos-01 mytest]# bzcat /tmp/3.txt.bz2
  • bzip2不可以压缩目录

xz压缩工具

[root@centos-01 mytest]# xz 2.txt 
[root@centos-01 mytest]# ls
2.txt.xz  3.txt  4.txt
[root@centos-01 mytest]# du -sh 2.txt.xz 
56K 2.txt.xz
[root@centos-01 mytest]# xz -d 2.txt.xz 
[root@centos-01 mytest]# ls
2.txt  3.txt  4.txt
[root@centos-01 mytest]# xz 2.txt 
[root@centos-01 mytest]# unxz 2.txt.xz 
[root@centos-01 mytest]# ls
2.txt  3.txt  4.txt
[root@centos-01 mytest]# ls /tmp/
1.txt      systemd-private-604486059e93474a8b695802ff294d42-chronyd.service-nvD2qs
2.txt.gz   systemd-private-604486059e93474a8b695802ff294d42-vgauthd.service-xKKbjw
3.txt.bz2  systemd-private-604486059e93474a8b695802ff294d42-vmtoolsd.service-yaJKgA
mytest
[root@centos-01 mytest]# xz -c 2.txt > /tmp/4.txt.xz
[root@centos-01 mytest]# xz -d -c /tmp/4.txt.xz > ./1.txt
[root@centos-01 mytest]# ll
总用量 5712
-rw-r--r--. 1 root root 1460358 54 20:42 1.txt
-rw-r--r--. 1 root root 1460358 54 10:49 2.txt
-rw-r--r--. 1 root root 1460358 54 20:28 3.txt
-rw-r--r--. 1 root root 1460358 54 20:30 4.txt
  • xzcat命令查看压缩文件内容
[root@centos-01 mytest]# xzcat /tmp/4.txt.xz

zip压缩工具

[root@centos-01 mytest]# tree /tmp/
/tmp/
├── 1.txt
├── 2.txt.gz
├── 3.txt.bz2
├── 4.txt.xz
├── mytest
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── yourtest
│       ├── 1.txt
│       ├── 2.txt
│       ├── 3.txt
│       └── 4.txt
├── systemd-private-604486059e93474a8b695802ff294d42-chronyd.service-nvD2qs
│   └── tmp
├── systemd-private-604486059e93474a8b695802ff294d42-vgauthd.service-xKKbjw
│   └── tmp
└── systemd-private-604486059e93474a8b695802ff294d42-vmtoolsd.service-yaJKgA
    └── tmp
        └── vmware-root

9 directories, 12 files
[root@centos-01 mytest]# du -sh yourtest/
5.6M    yourtest/
[root@centos-01 mytest]# zip 2.txt.zip 2.txt
-bash: zip: 未找到命令
[root@centos-01 mytest]# yum install -y zip
[root@centos-01 mytest]# zip 2.txt.zip 2.txt
  adding: 2.txt (deflated 74%)
[root@centos-01 mytest]# du -sh 2.txt.zip 
376K    2.txt.zip
[root@centos-01 mytest]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  yourtest
[root@centos-01 mytest]# zip -r yourtest.zip yourtest 3.txt 
  adding: yourtest/ (stored 0%)
  adding: yourtest/3.txt (deflated 74%)
  adding: yourtest/4.txt (deflated 74%)
  adding: yourtest/2.txt (deflated 74%)
  adding: yourtest/1.txt (deflated 74%)
  adding: 3.txt (deflated 74%)
[root@centos-01 mytest]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  yourtest  yourtest.zip
[root@centos-01 mytest]# du -sh yourtest.zip 
1.9M    yourtest.zip
[root@centos-01 mytest]# unzip
-bash: unzip: 未找到命令
[root@centos-01 mytest]# yum install -y unzip
[root@centos-01 mytest]# unzip yourtest.zip 
Archive:  yourtest.zip
replace yourtest/3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace yourtest/4.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  inflating: yourtest/4.txt          
replace yourtest/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A              
  inflating: yourtest/2.txt          
  inflating: yourtest/1.txt          
  inflating: 3.txt                   
[root@centos-01 mytest]# mkdir test
[root@centos-01 mytest]# unzip 2.txt.zip -d test/
Archive:  2.txt.zip
  inflating: test/2.txt              
[root@centos-01 mytest]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  test  yourtest  yourtest.zip
[root@centos-01 mytest]# unzip -l yourtest.zip 
Archive:  yourtest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  05-04-2018 21:27   yourtest/
  1460358  05-04-2018 21:27   yourtest/3.txt
  1460358  05-04-2018 21:27   yourtest/4.txt
  1460358  05-04-2018 21:27   yourtest/2.txt
  1460358  05-04-2018 21:27   yourtest/1.txt
  1460358  05-04-2018 20:28   3.txt
---------                     -------
  7301790                     6 files

tar打包工具

tar本身为一个打包工具,可以把目录打包成一个文件,它的好处是它把所有文件整合成一个大文件整体,方便拷贝或者移动。

语法:tar [-zjxcvfpP] filename

  • “-z”:同时用gzip压缩
  • “-j”:同时用bzip2压缩
  • “-x”:解包或者解压缩
  • “-t”:查看tar包里面的文件
  • “-c”:建立一个tar包或者压缩文件包
  • “-v”:可视化
  • “-f”:后面跟文件名,压缩时跟“-f filename”,意思是压缩后的文件名为filename,解压时跟“-f 文件名”,意思是解压filename。需要注意,如果是多个参数组合的情况下带有“-f”,请把“-f”写到最后面。
  • “-p”:使用原文件的属性,压缩前什么属性压缩后还什么属性。
  • “-P”:可以使用绝对路径。
  • --exclude filename:在打包或者压缩时,不要将filename文件包括在内。
[root@centos-01 mytest]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  test  yourtest  yourtest.zip
[root@centos-01 mytest]# tar -cvf yourtest.tar yourtest
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  test  yourtest  yourtest.tar  yourtest.zip
[root@centos-01 mytest]# tar -xvf yourtest.tar 
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# tar -tf yourtest.tar 
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# ls
1.txt  2.txt  2.txt.zip  3.txt  4.txt  test  yourtest  yourtest.tar  yourtest.zip
[root@centos-01 mytest]# tar -cvf yourtest.tar --exclude 1.txt yourtest
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt

tar打包并压缩

[root@centos-01 mytest]# tar -zcvf yourtest.tar.gz yourtest
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# du -sh yourtest.tar.gz 
1.5M    yourtest.tar.gz
[root@centos-01 mytest]# du -sh yourtest
5.6M    yourtest
[root@centos-01 mytest]# tar -jcvf yourtest.tar.bz2 yourtest
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# du -sh yourtest.tar.bz2 
544K    yourtest.tar.bz2
[root@centos-01 mytest]# tar -Jcvf yourtest.tar.xz yourtest
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# du -sh yourtest.tar.xz 
60K yourtest.tar.xz
[root@centos-01 mytest]# ls
1.txt  2.txt.zip  4.txt  yourtest      yourtest.tar.bz2  yourtest.tar.xz
2.txt  3.txt      test   yourtest.tar  yourtest.tar.gz   yourtest.zip
[root@centos-01 mytest]# tar -tf yourtest.tar.gz 
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# tar -tf yourtest.tar.bz2 
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt
[root@centos-01 mytest]# tar -tf yourtest.tar.xz 
yourtest/
yourtest/3.txt
yourtest/4.txt
yourtest/2.txt
yourtest/1.txt