数据压缩,就是在不丢失数据信息的前提下减少数据量的一种技术。
compress是一个古老的压缩工具,其压缩文件后缀为.Z。
-d: 解压缩-c: 结果输出至标准输出,不删除原文件
-v: 显示详情
使用示例
[root@centos7 /testdir]#compress passwd # 压缩
[root@centos7 /testdir]#ls
passwd.Z
[root@centos7 /testdir]#compress -d passwd # 解压
gzip压缩后的文件后缀为.gz,如果压缩的是tar备份文件,则扩展名为.tar.gz
gzip, gunzip, zcat - compress or expand files
-d: 解压缩,相当于unzip
-c: 将压缩或解压缩的结果输出至标准输出
-#:1-9,指定压缩比zcat: 不显示解压缩的前提下查看文本文件内容
gunzip用于解压缩
zcat用于查看
使用示例
[root@centos7 /testdir]#gzip passwd
[root@centos7 /testdir]#ls
passwd.gz passwd.Z
[root@centos7 /testdir]#
[root@centos7 /testdir]#zcat passwd.gz > passwd
[root@centos7 /testdir]#ls
passwd.gz passwd passwd.Z
[root@centos7 /testdir]#
bzip2压缩的文件的扩展名为.bz2
-k: keep,保留原文件
-d: 解压缩
-#:1-9,压缩比,默认为6bzcat: 不显示解压缩的前提下查看文件文件内容
bunzip用于解压缩
bzcat用于查看不解压缩
使用示例
[root@centos7 /testdir]#bzip2 passwd
[root@centos7 /testdir]#ls
passwd.gz passwd.bz2 passwd.Z[
root@centos7 /testdir]#bzcat passwd.bz2 > passwd
[root@centos7 /testdir]#ls
passwd.gz passwd passwd.bz2 passwd.Z
[root@centos7 /testdir]#
xz压缩后的文件扩展名为.xz
-k: keep,保留原文件
-d: 解压缩
-#:1-9,压缩比,默认为6xzcat: 不显示解压缩的前提下查看文件文件内容
unxz用于解压缩
xzcat用于查看
使用示例
[root@centos7 /testdir]#xz passwd
[root@centos7 /testdir]#ls
passwd.bz2 passwd.gz passwd.xz passwd.Z
[root@centos7 /testdir]#
[root@centos7 /testdir]#xzcat passwd.xz > passwd
[root@centos7 /testdir]#ls
passwd passwd.bz2 passwd.gz passwd.xz passwd.Z
[root@centos7 /testdir]#
打包压缩文件,经zip压缩后会另外生成.zip的文件而不删除原文件。
zip - package and compress (archive) files
-r: 递归处理,将指定目录下的所有文件与子目录一并处理
-q: 不显示执行过程
unzip用于解压缩
zcat用于查看
使用示例
[root@centos7 /testdir]#zip -q passwd ./passwd
[root@centos7 /testdir]#ls
passwd passwd.bz2 passwd.gz passwd.xz passwd.Z passwd.zip
[root@centos7 /testdir]#
看看大概的压缩情况:
[root@centos7 /testdir]#ll
total 192
-rw-r--r--. 1 root root 164065 Aug 19 09:06 message.zip
-rw-r--r--. 1 root root 4129 Aug 19 08:46 passwd
-rw-r--r--. 1 root root 1526 Aug 19 08:30 passwd.bz2
-rw-r--r--. 1 root root 1539 Aug 19 08:39 passwd.gz
-rw-r--r--. 1 root root 1540 Aug 19 08:45 passwd.xz
-rw-r--r--. 1 root root 2151 Aug 19 08:16 passwd.Z
-rw-r--r--. 1 root root 1676 Aug 19 09:02 passwd.zip
[root@centos7 /testdir]#
zcat命令可查看压缩的文件,但并不解压。
[root@bash ~]# zcat b.zip
#!/bin/bash
#在url中写入你的51cto博客网址,保存退出,运行脚本,可以根据需要自行修改
url=http://yolynn.blog.51cto.com/
tar命令可为文件或目录创建档案(备份文件),tar命令可将很多文件打包成一个文件,从而可结合压缩工具实现归档并压缩了。
使用语法:
tar [OPTION...] [FILE]...
EXAMPLES
tar -cf archive.tar foo bar
# Create archive.tar from files foo and bar.
tar -tvf archive.tar
# List all files in archive.tar verbosely.
tar -xf archive.tar
# Extract all files from archive.tar.
常用参数:
-c: --creat, 创建新的备份文件
-C dir:在特定的目录解压缩
-f: --file=ARCHIVE, 指定备份文件
-x: --extract, --get, 从备份文件中还原文件
-t: --list, 列出备份文件的内容
-v: --verbose
tar用法小结:
(1) 创建归档
tar -c -f /PATH/TO/SOMEFILE.tar FILE...
tar cf/PATH/TO/SOMEFILE.tar FILE...
(2) 查看归档文件中的文件列表
tar -t -f /PATH/TO/SOMEFILE.tar(3) 展开归档
tar -x -f /PATH/TO/SOMEFILE.tar
tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/
(4) 结合压缩工具实现:归档并压缩
-j: bzip2, -z: gzip, -J: xz
打包成tar包:
tar -cvf passwd.tar passwd 仅打包,不压缩 tar -zcvf passwd.tar.gz passwd 打包并以gzip压缩 tar -jcvf passwd.tar.bz2 passwd 打包并以bzip2压缩 tar -Jcvf passwd.tar.xz passwd 打包并以xz压缩
使用示例
[root@centos7 /testdir]#tar -cf passwd.tar passwd
[root@centos7 /testdir]#ls
passwd passwd.tar
[root @centos7 /testdir]#tar -zcf passwd.tar.gz passwd
[root@centos7 /testdir]#ls
passwd passwd.tar passwd.tar.gz
[root@centos7 /testdir]#tar -jcf passwd.tar.bz2 passwd
[root@centos7 /testdir]#ls
passwd passwd.tar passwd.tar.bz2 passwd.tar.gz
[root@centos7 /testdir]#tar -Jcf passwd.tar.xz passwd
[root@centos7 /testdir]#ls
passwd passwd.tar passwd.tar.bz2 passwd.tar.gz passwd.tar.xz
[root@centos7 /testdir]#
[root@centos7 /testdir]#tar -tvf passwd.tar # 查询
-rw-r--r-- root/root 10240 2016-08-19 09:27 passwd
[root@centos7 /testdir]#tar -tvf passwd.tar.gz
-rw-r--r-- root/root 10240 2016-08-19 09:27 passwd
[root@centos7 /testdir]#
[root@centos7 /testdir]#tar xf passwd.tar # 解压
[root@centos7 /testdir]#ls
passwd passwd.tar passwd.tar.bz2 passwd.tar.gz passwd.tar.xz
[root@centos7 /testdir]#tar xf passwd.tar.gz
[root@centos7 /testdir]#ls
passwd passwd.tar passwd.tar.bz2 passwd.tar.gz passwd.tar.xz
[root@centos7 /testdir]#
[root@centos7 /testdir]#ll
total 44
-rw-r--r--. 1 root root 10240 Aug 19 09:27 passwd
-rw-r--r--. 1 root root 20480 Aug 19 10:52 passwd.tar
-rw-r--r--. 1 root root 116 Aug 19 10:53 passwd.tar.bz2
-rw-r--r--. 1 root root 120 Aug 19 10:52 passwd.tar.gz
-rw-r--r--. 1 root root 180 Aug 19 10:53 passwd.tar.xz
cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以.cpio或者.tar结尾的文件;换言之,cpio可以复制文件到归档包中,或者从归档包中复制文件。
使用语法:
cpio - copy files to and from archives
cpio[选项] > 文件名或者设备名
cpio[选项] < 文件名或者设备名
EXAMPLES
% ls | cpio -ov > directory.cpio #必须要在当前工作目录中执行ls,后面接绝对路径会报错
% find . -print -depth | cpio -ov > tree.cpio
% cpio -iv < directory.cpio
% cpio -idv < tree.cpio
% find . -depth -print0 | cpio --null -pvd new-dir
常用参数:
-o: --create,Run in copy-out mode,将文件拷贝打包成文件或者将文件输出到设备上
-i: --extract,Run in copy-in mode,解包,将打包文件解压或将设备上的备份还原到系统
-t: 预览,查看文件内容或者输出到设备上的文件内容
-v: 显示打包过程中的文件名称。
-d: 解包生成目录,在cpio还原时,自动的建立目录
-c: 一种较新的存储方式
使用示例
[root@centos7 /]#find ./etc |cpio -o > etc.cpio # 备份/etc目录
wKiom1e3MfKArn2SAABJ8zL76mY046.png
[root@centos7 /testdir]#find /etc/issue |cpio -o >issue.cpio1 block
[root@centos7 /testdir]#lsissue.cpio
[root@centos7 /testdir]#cpio -tv <issue.cpio # 显示预览
-rw-r--r-- 1 root root 23 Dec 9 2015 /etc/issue1 block
[root@centos7 /testdir]#
cpio在打包备份时用的是绝对路径,且cpio无法直接读取文件,它需要每个文件或目录的完整路径名才能读取识别,故cpio命令一般与find配合使用。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。