Linux file compression and packaging

Ready to work

[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 compression tool

Syntax: gzip [-dn] filename(n is a number from 1-9)

  • "-d": use when decompressing
  • "-n" (n is a number from 1-9): Compression level, the first level is the fastest compression, but the least amount of compression; the 9th level is the slowest compression, but the greatest amount of compression. The default level is 6, " --fast" and " --best" are equivalent to "-1" and "-9" respectively.
[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 command to view the contents of compressed files
[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 does not support compressing directories.

bzip2 compression tool

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

grammar:bzip2 [-dz] filename

  • "-d": decompress
  • "-z": compress
[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 command to view the contents of compressed files
[root@centos-01 mytest]# bzcat /tmp/3.txt.bz2
  • bzip2 cannot compress directories

xz compression tool

[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 command to view the contents of compressed files
[root@centos-01 mytest]# xzcat /tmp/4.txt.xz

zip compression tool

[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 packaging tool

tar itself is a packaging tool that can package a directory into a file. Its advantage is that it integrates all files into a large file as a whole, which is convenient for copying or moving.

grammar:tar [-zjxcvfpP] filename

  • "-z": also compress with gzip
  • "-j": also compress with bzip2
  • "-x": unpack or unpack
  • "-t": View the files in the tar package
  • "-c": create a tar package or compressed file package
  • "-v": visualize
  • "-f": followed by the file name, followed by "-f filename" when compressing, which means the compressed file name is filename, and "-f filename" when decompressing, which means decompressing filename. It should be noted that if there is "-f" in the case of a combination of multiple parameters, please write "-f" at the end.
  • "-p": Use the attributes of the original file, what attributes before compression and what attributes will remain after compression.
  • "-P": An absolute path can be used.
  • --exclude filename: Do not include the filename file when packing or compressing.
[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 pack and compress

[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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763830&siteId=291194637