压缩工具类型
压缩 解压缩 压缩文件后缀
1.compress uncompress "z"
2.gzip gunziip "gz"
3.bzip bunzip2 "bz2"
4.xz unxz "xz"
5.zip unzip "zip"
6.lzma unlzma "lzma"
7.tar/cip
gzip/gunzip/zcat
gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ... ] 压缩并删除原文件
gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ] 解压缩并删除原文件
zcat [ -fhLV ] [ name ... ] 查看压缩文件内清单
例:[root@Centos-7-24 opt]# ll -h
total 476K
-rw-------. 1 root root 476K Oct 3 09:52 messages
[root@Centos-7-24 opt]# gzip messages
[root@Centos-7-24 opt]# ll -h
total 64K
-rw-------. 1 root root 64K Oct 3 09:52 messages.gz --压缩并删除原文件
查看压缩后的文件:zcat messages.gz--只适用查看小文件,是将压缩文件临时解压到一位置并进行查看
gzip -d=gunzip 解压缩,解压完成后删除压缩文件;
gzip -# 指定压缩比例,默认为6,数字越大压缩比越大;
gzip -c 将压缩结果标准输出 gzip -c file >/PATH/TO/SOMEFILE.gz
例
[root@Centos-7-24 opt]# gzip -c messages >messages.gz --用来压缩保留原文件
bzip2/bunzip2/bzcat
bzip2 [ -cdfkqstvzVL123456789 ] [ filenames ... ]
bunzip2 [ -fkvsVL ] [ filenames ... ]
bzcat [ -s ] [ filenames ... ]
bzip2recover filename
bzip2 -d 解压缩并删除原文件
bzip2 -# 指定压缩比,默认为6,数字越大压缩比越大(1-9)
bzip2 -k:keep 会自动保留原谅
bzcat 查看压缩文件内容清单
xz/unxz/xzcat
xz 压缩
xz -d 解压缩并删除原文件
xz -#指定压缩比例,默认为6,数字越大压缩比例越大(1-9)
xz -k 解压缩时保留原文件
归档工具tar/cpio 对多个文件或目录进行打包
1.创建归档时不会删除原文件
tar -c -f /PATH/TO/SMEFILE.tar file
例:
[root@Centos-7-24 opt]# tar -cf ./mydata.tar test/*.log
[root@Centos-7-24 opt]# ll
total 580
-rw-------. 1 root root 487026 Oct 3 10:04 messages
-rw-r--r--. 1 root root 64986 Oct 3 10:05 messages.gz
-rw-r--r--. 1 root root 40960 Oct 3 10:30 mydata.tar
drwxr-xr-x. 2 root root 255 Oct 3 10:28 test
2.展开归档文件
tar -xf /PATH/TO/SOMEFILE.tar file
tar -xf /PATH/TO/SOMEFILE.tar -C /PATH/TO/SOMEFILE 指定解压目录
例
[root@Centos-7-24 test]# tar xf ok1.tar
[root@Centos-7-24 test]# ll
total 72
-rw-------. 1 root root 0 Oct 3 10:28 boot.log
-rw-r--r--. 1 root root 407 Oct 3 10:39 fstab
-rw-r--r--. 1 root root 10240 Oct 3 10:40 ok1.tar
-rw-r--r--. 1 root root 986 Oct 3 10:39 passwd
3.查看归档文件中的文件列表
tar -tf /PATH/TO/SOMEFILE.tar 查看归档文件中的文件列表
例
[root@Centos-7-24 test]# tar tf ok1.tar
fstab
passwd
归档完成后需要压缩,结合此前的压缩工具,就能实现压缩多个文件
4.归档并压缩,能对目录直接操作
tar -z 调用gzip2
tar -zcf /PATH/TO/SOMEFILE.tar.gz FILE 归档并压缩
tar -zxf /PATH/TO/SOMEFILE.tar.gz 解压并展开归档
例:
[root@Centos-7-24 opt]# tar -zcf /opt/test.tar.gz test
[root@Centos-7-24 opt]# ll
total 1092
-rw-r--r--. 1 root root 407 Oct 3 10:39 fstab
-rw-r--r--. 1 root root 10240 Oct 3 10:39 fstab.tar
-rw-------. 1 root root 487026 Oct 3 10:04 messages
-rw-r--r--. 1 root root 64986 Oct 3 10:05 messages.gz
-rw-r--r--. 1 root root 491520 Oct 3 10:36 messages.tar
-rw-r--r--. 1 root root 40960 Oct 3 10:30 mydata.tar
-rw-r--r--. 1 root root 986 Oct 3 10:39 passwd
drwxr-xr-x. 2 root root 4096 Oct 3 10:41 test
-rw-r--r--. 1 root root 6144 Oct 3 10:52 test.tar.gz
-j :调用bzip2
tar -jcf/-jxf
-J:调用xz
tar -Jcf/-Jxf
zip:即能归档又能压缩