linux下文件的压缩
常用命令集合
一、Linux常见压缩文件
.gz .bz2 .xz .zip,前三个不能干文件夹,zip跟windows下那个一样。
默认模式下压缩成绩
按压缩率排
xz>bz2>gz≈zip
按CPU占用排列
zip≈gz<bz2<xz
注意随文件不同,效果不一样。
文件查找 find、locate
二、各种压缩处理工具处理文件方式(加压/解压)
通用命令格式
加压:命令 -#(级别,基本不用) -c 文件路径 > 文件路径
解压:命令 -d -c 文件路径 > 文件路径
find
find: 文件查找,针对文件名,精确查找,磁盘搜索,io读写,cpu开销大
find [options] [path...] [expression] [action]
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
s - socket文件
-size n[cwbkMG] : 文件大小 为 n 个由后缀决定的数据块。其中后缀为:
b: 代表 512 位元组的区块(如果用户没有指定后缀,则默认为 b)
c: 表示字节数
k: 表示 kilo bytes (1024字节)
w: 字 (2字节)
M:兆字节(1048576字节)
G: 千兆字节 (1073741824字节)
-depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-maxdepth 查找最大目录层数 如 1,即只查找一层目录
-fstype 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件
/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount 在查找文件时不跨越文件系统mount点。
-follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
===expression===
1、gzip 注意不能压缩文件夹
加压:
压缩前
按文件名:
[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0" //-i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-eth*"
压缩命令: gzip 文件名
按文件大小:
[root@localhost ~]# find /etc -size +5M //大于5M
[root@localhost ~]# find /etc -size 5M
[root@localhost ~]# find /etc -size -5M
[root@localhost ~]# find /etc -size +5M -ls //-ls找到的处理动作
指定压缩等级: gzip -#(1-9默认是6,基本很少用这个选项)
1级时
9级时
上下差距不大
指定查找的目录深度:
-maxdepth levels
-mindepth levels
[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-eth0"
解压
命令
gzip -d 文件名 或者 gungzip 文件名
按时间找(atime,mtime,ctime):
[root@localhost ~]# find /etc -mtime +5 //修改时间超过5天
[root@localhost ~]# find /etc -mtime 5 //修改时间等于5天
[root@localhost ~]# find /etc -mtime -5 //修改时间5天以内
指定定处理文件的路径,不删除原文件
加压gzip -c 文件路径 > 想要放的路径
解压gzip -c -d 文件路径 > 想要放的路径
按文件属主、属组找:
[root@localhost ~]# find /home -user jack //属主是jack的文件
[root@localhost ~]# find /home -group hr //属组是hr组的文件
[root@localhost ~]# find /home -user jack -group hr
[root@localhost ~]# find /home -user jack -a -group hr
[root@localhost ~]# find /home -user jack -o -group hr
[root@localhost ~]# find /home -nouser
[root@localhost ~]# find /home -nogroup
[root@localhost ~]# find /home -nouser -o -nogroup
2、bzip2
相较于gzip压缩率更高,CPU占有率也要大一下,非系统自带,需要yum安装。不能压文件夹。
操作与gzip完全一样
加压
bzip2 文件名
解压
按文件类型:
[root@localhost ~]# find /dev -type f //f普通
[root@localhost ~]# find /dev -type d //d目录
[root@localhost ~]# find /dev -type l //l链接
[root@localhost ~]# find /dev -type b //b块设备
[root@localhost ~]# find /dev -type c //c字符设备
[root@localhost ~]# find /dev -type s //s套接字
[root@localhost ~]# find /dev -type p //p管道文件
3、xz
tar包中常见,压缩率比bzip2更高,操作跟大家一样。
加压
解压
按文件权限:
[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find . -perm -222 -ls //全局可写
[root@localhost ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
[root@localhost ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
[root@localhost ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky
4、zip
跟windows下那款差不多,操作跟上面那三个不同,他可以压缩目录。zip的压缩工具和解压缩工具都需要单独重新安装。
按正则表达式:
-regex pattern
[root@localhost ~]# find /etc -regex '.*ifcfg-eth[0-9]'
.* 任意多个字符
[0-9] 任意一个数字
[root@localhost ~]# find /etc -regex '.*ifcfg-enp0s25'
/etc/sysconfig/network-scripts/ifcfg-enp0s25
[root@localhost ~]# find /etc -regex '.*ifcfg-enp0s[0-9]+'
/etc/sysconfig/network-scripts/ifcfg-enp0s25
==找到后处理的动作 ACTIONS: (默认动作-print)==
-print
-ls
-delete
-exec 后面跟自定义的shell命令
-ok 后面跟自定义的shell命令
[root@localhost ~]# find /etc -name "ifcfg*"
[root@localhost ~]# find /etc -name "ifcfg*" -print
[root@localhost ~]# find /etc -name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp ;
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp ;
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} ;
[root@localhost ~]# find /etc -name "ifcfg*" -delete
扩展知识:find结合xargs
[root@localhost ~]# find . -name "yang*.txt" |xargs rm -rf
[root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
操作示例:
(1)压文件