目录
1.which :命令查找
查找命令的位置:which
命令、whereis
命令
[root@localhost ~]# which ls
[root@localhost ~]# whereis vim
2.locate:文件查找,依赖数据库
文件查找,依赖数据库,并且有排除目录/etc/updatedb.conf/
locate查找文件hosts:
[root@localhost ~]# locate hosts
更新locate数据库:
[root@localhost ~]# updatedb
3.find: 文件查找,针对文件名
3.1.find 语法:
find [path...] [options] [expression] [action]
命令 路径 选项 表达式 动作
3.2.按时间:
- 访问时间 (-atime/天,-amin/分钟):用户最近一次访问时间。
- 修改时间(-mtime/天,-mmin/分钟):文件最后一次被修改的时间。
- 变化时间(-ctime/天,-cmin/分钟):文件元数据(例如权限等)。
Eg:
查找最近10天内被访问过的所有文件:
[root@localhost ~]# find . -type f -atime -10
查找超过10天内被访问过的所有文件:
[root@localhost ~]# find . -type f -atime +10
查找访问时间超过20分钟的所有文件:
[root@localhost ~]# find . -type f -amin +20
找出比linkage修改时间更长的所有文件:
[root@localhost ~]# find . -type f -newer linkage
3.3.按文件名:
[root@localhost ~]# find /etc -name "hosts"
[root@localhost ~]# find /etc -iname "hosts"
[root@localhost ~]# find /etc -iname "hos*" //-i忽略大小写
3.4.按文件大小:
[root@localhost ~]# find /etc -size +5M //文件>5M
[root@localhost ~]# find /etc -size 5M //文件=5M
[root@localhost ~]# find /etc -size -5M //文件<5M
3.5.按指定查找的目录深度:
[root@localhost ~]# find / -maxdepth 4 -a -name "ifcfg-en*" //-a是and
3.6.按文件属主、属组找:
(前提:查找的用户和组已存在)
[root@localhost ~]# find /home -user jack //属主是jack的文件
[root@localhost ~]# find /home -group hr //属组是hr组的文件
3.7.按文件类型:
[root@localhost ~]# find /tmp -type f
f
普通文件、b
块设备文件、d
目录、p
管道文件、l
链接文件
3.8.按文件权限:
[root@localhost ~]# find . -perm 644
3.9.找到后处理的动作 ACTIONS:(默认是显示文件)
[root@localhost ~]# find . -perm 715 -print //显示文件
[root@localhost ~]# find . -perm 715 -ls //显示文件属性
[root@localhost ~]# find /etc -name "775*" -delete //找到后删除
[root@localhost ~]# find /etc -name "775*" rm -rf {} \; //找到后删除
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; //找到后复制
(-ok 改为-exec 不再询问)