linux小白之路之文件查找find命令

查找分为三类:

1.which:命令查找
2.find:文件查找,针对文件名
3.locate:文件查找,依赖数据库

一、命令文件查找

如 查找ls命令的位置
which ls
/usr/bin/ls //结果

我们可以看出命令所在目录在/usr/bin/下

二、文件查找find

语法:find [path…] [ options] [ expression] [action]
      命令,   路径,    选项,  表达式     , 动作

1.按文件名来查找

 [root@localhost ~]# find /etc -name "hosts"
 /etc/hosts
 /etc/avahi/hosts
[root@localhost ~]# find /etc/ -iname "hosts"

注意:-i忽略大小写来查找 结果输出 /etc/hosts 文件查找成功

2.按文件大小来查找

[root@localhost ~]# find /etc -size +5M
/etc/udev/hwdb.bin
[root@localhost ~]# find /etc/ -size 5M
[root@localhost ~]# find /etc/ -size -5M
/etc/
/etc/fstab
/etc/crypttab

说明:+5M是大于5M以上的文件 ,5M是大小是5M的文件 , -5M是小于5M的文件

3.按文件目录深度查找

[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-en*"
/etc/sysconfig/network-scripts/ifcfg-ens33

说明:里面的数字4 是目录深度

4.按文件属主、属组查找

[root@localhost ~]# find /home -user    //后面接属主是某某某的文件
[root@localhost ~]# find /home -group    //后面接属组是某某某的文件

5.按文件类型查找

[root@localhost ~]# find /tmp -type f   //f是普通文件

[root@localhost ~]# find /dev/ -type b     // b是设备文件

6.按文件权限查找

[root@localhost ~]# find .-perm 644 -ls  //-ls是find的动作之一,精确权限

7.找到后处理的动作(删除或复制)

find /etc/-name "你想删的文件名" -delete     //找到后删除
find /etc/-name "ifcfg*" -ok cp -rvf {} /tmp \;     //找到后复制

以上内容就是find常用命令 欢迎大家评论留言,指出错误,我会积极改正。谢谢

发布了1 篇原创文章 · 获赞 8 · 访问量 113

猜你喜欢

转载自blog.csdn.net/weixin_45508789/article/details/104482364