Linux基础教程文件查找

命令文件

which命令,作用:查找命令的绝对路径
which会在PATH变量所对应的目录里找,找到了就把绝对路径显示出来.
PATH变量可以使用echo $PATH查看

任意文件

locate命令(查询的数据库: /var/lib/mlocate/mlocate.db, 手动更新数据库: updatedb
[root@localhost ~]# locate ifcfg-enp

find命令

语法: find [options] [path…] [expression]

按文件名:
[root@localhost ~]# find /etc -name “ifcfg-eth0”
[root@localhost ~]# find /etc -iname “ifcfg-eth0” //-i忽略大小写
[root@localhost ~]# find /etc -iname “ifcfg-eth*” //ifcfg-eth开头

按文件大小:
[root@localhost ~]# find /etc -size +5M //大于5M
[root@localhost ~]# find /etc -size 5M
[root@localhost ~]# find /etc -size -5M

指定查找的目录深度:

-maxdepth levels
[root@localhost ~]# find / -maxdepth 3 -a -name “ifcfg-eth0” //根下三层以内找ifcfg-eth0文件

按时间找(atime,mtime,ctime):
[root@localhost ~]# find /etc -mtime +5 //修改时间超过5天
[root@localhost ~]# find /etc -mtime 5 //修改时间等于5天
[root@localhost ~]# find /etc -mtime -5 //修改时间5天以内

按文件属主、属组找:
[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 //查找无主文件

按文件类型:
[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管道文件

根据inode查找:-inum n
[root@localhost ~]# find / -inum 31064284

按文件权限:
带“-”代表查找大于等于规定权限的文件,000为不关心,比如4000,只是查找有sgid的文件,其他权限不关心

[root@localhost ~]# find . -perm 644 //不带“-”是指必须满足规定权限
[root@localhost ~]# find . -perm -644
[root@localhost ~]# find . -perm -600
[root@localhost ~]# find /sbin -perm -4000 //包含set uid
[root@localhost ~]# find /sbin -perm -2000 //包含set gid
[root@localhost ~]# find /sbin -perm -1000 //包含sticky

找到后处理的动作:

-print 找到后显示
-ls 找到后显示文件属性
-delete 找到后删除(慎用!!!)
-exec 找到后执行什么命令
-ok 找到以交互的方式执行后面的命令
ok和exec都可以在后面跟一个操作命令

[root@localhost ~]# find /etc -name “ifcfg*” -print
[root@localhost ~]# find /etc -name “ifcfg*” -ls
[root@localhost ~]# find /etc -name “ifcfg*” -exec cp -rf {} /tmp \; //以分号为结束符,加转义符号
[root@localhost ~]# find /etc -name “ifcfg*” -ok cp -rf {} /tmp \;

[root@localhost ~]# find /etc -name “ifcfg*” -exec rm -rf {} \;
// \后面还可以跟+,“;”代表对找到的文件逐一处理,“+”表示一起处理,但是+不是每个命令都能使用,所以一般用“;”
[root@localhost ~]# find /etc -name “ifcfg*” -delete

扩展知识:find结合xargs
[root@localhost ~]# find . -name “uplooking*.txt” | xargs rm -rf
[root@localhost ~]# find . -name “uplooking*.txt” -exec rm -rf {} \;
[root@localhost ~]# find /etc -name “ifcfg-eth0” | xargs -I {} cp -rf {} /var/tmp
//{}只是一个字符串,代表前面收到的内容,只要保证两个命令后面的{}一致就可以了,也可以用其他字符串,但要保证一致

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容