Linux下的文件查找命令

简介:
Linux的五个文件查找命令:find,locate,whereis,which,type

find:查找文件或目录所在路径 

 
  1. 格式:find [路径] [表达式] 
  2. 表达式: 
  3. -name    :查找名为filename的文件 
  4. -perm    :按执行权限来查找 
  5. -empty   :查找空文件或空目录 
  6. -user    :按文件属主来查找 
  7. -group   :按组来查找 
  8. -nogroup   :查无有效属组的文件,即文件的属组在/etc/groups中不存在 
  9. -nouser  :查无有效属主的文件,即文件的属主在/etc/passwd中不存 
  10. -mtime   :按文件更改时间来查找文件 
  11. -atime   :按文件访问时间来查找文件 
  12. -ctime   :按文件创建时间来查找文件 
  13. -newer     :查更改时间更新的文件或目录 
  14. -type    :查是块设备b、目录d、字符设备c、管道p、符号链接l、普通文件f 
  15. -size n[c] :查找大小为n块(512字节)[或n字节]的文件 
  16. -inum    :根据i节点查找 
  17. -depth   :使查找在进入子目录前先行查找完本目录 
  18. -fstype  :查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到 
  19. -mount   :查文件时不跨越文件系统mount点 
  20. -cpio    :对匹配的文件使用cpio命令,将他们备份到磁带设备中 
  21. -prune   :忽略某个目录 
  22. -maxdepth  :查询的目录深度 
  23.  
  24. -exec   :查找文件并执行后面的命令 find ... -exec CMD {} \; 
  25. -ok     :询问是否要执行后面的命令 find ... -ok CMD {} \;  
-perm mode 表示严格匹配
-perm -mode         表示mode中转换成二进制的1必须全部匹配(不管0位) 
-perm +mode         表示mode中转换成二进制的1必须部分匹配(不管0位) 

-ctime/atime/mtime/cmin/amin/mmin:按时间查找
以天为单位的 :ctime、atime、mtime
以分钟为单位的 :cmin、amin、mmin
c--change 表示文件的属性被修改过
a--access
m--modify 表示文件的内容被修改过
+n 表示n天以前
-n 表示n天以内
 
 
  1. [root@rhel6 ~]# find /etc/ -name "host*"                 "查询/etc/目录(包括子目录)中以host开头的文件或目录" 
  2. [root@rhel6 ~]# find -type l                         "查询当前目录下文件类型为链接的文件" 
  3. [root@rhel6 ~]# find -size +10000000c                "查询当前目录中>10M的文件" 
  4. [root@rhel6 ~]# find -size -1K                       "查询当前目录中<1K的文件" 
  5. [root@rhel6 ~]# find /etc -name inittab -o -size +17M    "查询/etc/目录中文件名为inittab或文件>17M的文件" 
  6. [root@rhel6 ~]# find /etc -name "*.conf" [-a] -size +20k "查询/etc/目录中文件名为*.conf且文件<20k的文件" 
  7. [root@rhel6 ~]# find /etc/* -name "*.conf" -not -name "*http*"  "查询/etc目录中文件名为*.conf但不包含http的文件"     
  8. [root@rhel6 ~]# find /etc/ -empty                    "查询/etc/目录中的空文件或空目录" 
  9. [root@rhel6 ~]# find /var -user oracle                   "查询/var/目录中属于用户oracle的文件或目录" 
  10. [root@rhel6 ~]# find /home -group xfcy   
  11. [root@rhel6 ~]# find -inum 1024                  "查询当前目录中 i 节点为1024的文件或目录" 
  12. [root@rhel6 ~]# find -newer new                  "查询当前目录中比文件new还新的文件或目录" 
  13. [root@rhel6 ~]# find /etc/ -nouser -o -nogroup           "查询/etc/目录中不属于本地用户的文件或目录(危险文件)" 
  14. [root@rhel6 ~]# find /data/ -mmin -10                "查询/data/目录中十分钟内文件内容被修改过的文件" 
  15. [root@rhel6 ~]# find /proc/ -type f -maxdepth 1          "查询/data/目录中文件类型为普通文件的文件且不查询子目录" 
  16.  
  17. [root@rhel6 ~]# find /data/ -mtime -10 -exec rm {} \;    "查询/data/目录中十分钟内内容被修改过的文件并将其删除" 
  18. [root@rhel6 ~]# find /data/ -mtime -10 -ok rm {} \;      "查询/data/目录中十分钟内内容被修改过的文件并询问是否将其删除(y/n)" 
 =================================================================================
locate:根据文件数据库updatedb查找文件或目录
locate不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息(默认没有扫描外接的移动硬盘或者挂载在/media下的其他分区).
Linux系统自动创建这个数据库,并且每天自动更新一次(crontab),所以使用locate命令查不到最新变动过的文件.为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库.
 
  1. [root@rhel6 ~]# locate /etc/sh                   "查询/etc目录下所有以sh开头的文件" 
 ====================================================================================
whereis:只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。
 
  1. [root@rhel6 ~]# whereis find 
  2. find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/man/man1p/find.1p.gz 
  3. [root@rhel6 ~]# whereis -b find 
  4. find: /usr/bin/find 
====================================================================================
which:在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。即使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
 
 
 
  1. [root@rhel6 ~]# which find 
  2. /usr/bin/find 
====================================================================================
type:其实不能算查找命令,它是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。如果一个命令是外部命令,那么使用-p参数,会显示该命令的路径,相当于which命令。
 
 
  
  1. [root@rhel6 ~]# type cd 
  2. cd is a shell builtin 
  3. [root@rhel6 ~]# type -p find
  4. /bin/find


本文转自Vnimos51CTO博客,原文链接:http://blog.51cto.com/vnimos/1161355 ,如需转载请自行联系原作者


相关文章
|
1天前
|
Linux Shell
Linux操作系统下查找大文件或目录的技巧
Linux操作系统下查找大文件或目录的技巧
|
1天前
|
算法 Linux
Linux:文件增删 & 文件压缩指令
Linux:文件增删 & 文件压缩指令
5 0
|
1天前
|
安全 Linux 测试技术
|
1天前
|
安全 Linux Windows
Linux中Shutdown命令使用介绍
Linux中Shutdown命令使用介绍
|
2天前
|
缓存 关系型数据库 Linux
Linux目录结构:深入理解与命令创建指南
Linux目录结构:深入理解与命令创建指南
|
2天前
|
数据挖掘 Linux vr&ar
Linux命令实战:解决日常问题的利器
Linux命令实战:解决日常问题的利器
|
2天前
|
NoSQL Linux Redis
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
15 0
|
2天前
|
安全 Linux Shell
linux基础命令详解
linux基础命令详解
8 0
|
3天前
|
Linux
在 Linux 中创建文件
在 Linux 中创建文件
|
3天前
|
Linux
Linux常用命令2
Linux常用命令2
5 0