一,文件查找
1,简介
grep:文件内容过滤
find:文件查找,针对文件名;locate:文件查询,依赖数据库
which:命令查询
2,命令文件查找
查找ls 命令的位置
# which ls //从PATH环境变量
或者
# whereis vim
3,任意文件查找
locate
A. locate (查询的数据库: /var/lib/mlocate/mlocate.db)
计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
手动更新数据库:updatedb
#touch abc.txt
#locate abc.txt
无,为什么
#updatedb
#locate abc.txt
有,为什么
#rm -rf abc.txt
#locate abc.txt
有,为什么
#updatedb
#locate abc.txt
无,为什么
如果你的机器没有locate命令,你该怎么办呢?
yum provides locate
yum install -y mlocate
注意:
因为配置文件中有排除查找路径。
/etc/updatedb.conf
第一行PRUNE_BIND_MOUNTS="yes"的意思是:是否进行限制搜索。
第二行是排除检索的文件系统类型,即列出的文件系统类型不进行检索。
第二行表示对哪些后缀的文件排除检索,也就是列在这里面的后缀的文件跳过不进行检索。不同后缀之间用空格隔开。
第四行是排除检索的路径,即列出的路径下的文件和子文件夹均跳过不进行检索。updatedb之后使用locate仍然找不到想要文件
find
语法
find [path...][options][expression][action]
按文件名
[root@tianyun ~]# find /etc -name "ifcfg-ens32"
[root@tianyun ~]# find /etc -iname "ifcfg-ens32" //-i忽略大小写
[root@tianyun ~]# find /etc -iname "ifcfg-ens*"
按文件大小
[root@tianyun ~]# find /etc -size +5M //大于5M
[root@tianyun ~]# find /etc -size 5M //等于5M
[root@tianyun ~]# find /etc -size -5M //小于5M
[root@tianyun ~]# find /etc -size +5M -ls //-ls找到的处理动作
指定查找的目录深度
-maxdepth levels
-mindepth levels
[root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-en*"
[root@tianyun ~]# find / -maxdepth 4 -a -name "ifcfg-en*"
按时间找(atime,mtime,ctime)
时间的概念
atime:(access time)显示的是文件中的数据最后被访问的时间,比如系统的进程直接使用或通过一些命令和脚本间接使用。(执行一些可执行文件或脚本)
mtime: (modify time)显示的是文件内容被修改的最后时间,比如用vi编辑时就会被改变。(也就是Block的内容)
ctime: (change time)显示的是文件的权限、拥有者、所属的组、链接数发生改变时的时间。当然当内容改变时也会随之改变(即inode内容发生改变和Block内容发生改变时)
案例
按时间找(atime,mtime,ctime):
[root@tianyun ~]# find /etc -mtime +5 //修改时间超过5天
[root@tianyun ~]# find /etc -mtime 5 //修改时间等于5天
[root@tianyun ~]# find /etc -mtime -5 //修改时间5天以内
按文件属性、属组找
[root@tianyun ~]# find /home -user jack //属主是jack的文件
[root@tianyun ~]# find /home -group hr //属组是hr组的文件
[root@tianyun ~]# find /home -user jack -group hr //和
[root@tianyun ~]# find /home -user jack -a -group hr //-a和
[root@tianyun ~]# find /home -user jack -o -group hr //-o或
[root@tianyun ~]# find /home -nouser
[root@tianyun ~]# find /home -nogroup
[root@tianyun ~]# find /home -nouser -o -nogroup
按文件类型(了解)
[root@tianyun ~]# find /dev -type f //f普通
[root@tianyun ~]# find /dev -type d //d目录
[root@tianyun ~]# find /dev -type l //l链接
[root@tianyun ~]# find /dev -type b //b块设备
[root@tianyun ~]# find /dev -type c //c字符设备
[root@tianyun ~]# find /dev -type s //s套接字
[root@tianyun ~]# find /dev -type p //p管道文件
按文件权限
普通权限
[root@tianyun ~]# find . -perm 644 -ls //精确权限
[root@tianyun ~]# find . -perm -644 -ls //包含权限即可
特殊权限
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky
按正则表达式(了解)
-regex pattern
[root@tianyun ~]# find /etc/ -regex '.*ifcfg-ens3[0-9]'
[0-9] 任意一个数字
找到后处理的动作ACTIONS
类型
-print 打印,默认选项
-ls
-delete
-exec 后面跟自定义的shell命令
-ok 后面跟自定义的shell命令
示例
# find /etc -name "ifcfg*"
# find /etc -name "ifcfg*" -print
# find /etc -name "ifcfg*" -ls
# find /etc -name "775*" -delete /775.txt是自定义文件
# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; //不提示
# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; //提示
# find /etc -name "775*" -exec rm -rf {} \;
find结合xargs
简介
之所以能用到xargs这个命令,是由于很多命令不支持|管道来传递参数
示例
扩展知识:find结合xargs
[root@tianyun ~]# find . -name "yang*.txt" |xargs rm -rf
[root@tianyun ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
二,文件打包及压缩
1,简介
tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。建议针对目录
2,思考
Demo:复制未打包的文件到远程主机。
time scp -r /etc root@192.168.122.172:/tmp
du -sh /etc
3,打包,压缩
语法:tar 选项 压缩包名称 源文件
===打包,压缩===
# tar -czf etc-gzip.tar.gz /etc/ //z是gzip
# tar -cjf etc-bzip.tar.bz /etc/ //j是bzip
# tar -cJf etc-xzip.tar.xz /etc/ //J是xzip
观察三个包的体积。
# ll -h etc*
-rw-r--r--. 1 root root 11M 10月 14 10:07 etc-gzip.tar.gz
-rw-r--r--. 1 root root 8.9M 10月 14 10:08 etc-bzip.tar.bz
-rw-r--r--. 1 root root 7.6M 10月 14 10:08 etc-xzip.tar.xz
压缩速度和压缩体积成反比。
file etc.tar.gz
4,压缩,解包
===解压,解包===# tar -tf sys.tar.xz //t查看f文件名
# tar -xzvf etc1.tar.gz //x解压z调gzip,还可以自动判断
# tar -xvf etc1.tar.gz //无需指定解压工具,tar会自动判断
# tar -xvf etc2.tar.bz2 -C /tmp //-C重定向到//tmp目录
终极大法:
# tar xf etc3.tar.xz //简单粗暴
5,解压Zip
==解压zip# unzip xxx.zip
6,案例
案例1:mysql物理备份及恢复
yum -y install mariadb-server
systemctl start mariadb
mkdir /backup
tar -cJf /backup/mysql.tar.xz /var/lib/mysql
rm -rf /var/lib/mysql
cd /var/lib/mysql
tar -xf /backup/mysql.tar.xz -C /
案例2:mysql物理备份及恢复
[root@localhost ~]# cd /var/lib/mysql
[root@localhost mysql]# tar -cJf /backup/mysql.tar.xz *
[root@localhost mysql]# tar -xf /backup/mysql.tar.xz -C /var/lib/mysql
进入工作目录,备份就不会包含绝对路径。案例1和2注意路径问题。
案例3:host A /etc (海量小文件) --------> host A /tmp
[root@localhost ~]# tar -czf - /etc |tar -xzf - -C /tmp
czf 后的“-”代表先保存到内存中。
xzf后的"-"指的是前面的内存数据
案例4:host A /etc (海量小文件) --------> host B /tmp
常规方法:
[root@localhost ~]# scp -r /etc 172.16.20.21:/tmp
扩展方法nc方法:
==host B 监听端口(192.168.100.20:8888)==
[root@hostb ~]# systemctl stop firewalld.service
[root@hostb ~]# nc -l 8888 |tar -xzf - -C /tmp //启动监听程序8888
==host A 连接B 的端口==
[root@localhost ~]# tar -czf - /etc | nc 192.168.100.20 8888
tar: Removing leading `/' from member names