find

find命令

find是Linux/Unix系统中查找文件的工具。命令格式如下

find [path...] [expression]

默认的path是当前目录,默认的expression是-print,expression可以划分为四类:operators、options、tests、actions

expression

这里列出的是经常会用到的参数。find命令还有很多参数,具体可以看manual手册。

operators
表示表达式之间的逻辑关系,默认是and的关系,常用的两个参数是非和或

  • -o/-or :或
  • -not/! :非

options
-maxdepth 搜索目录的深度

tests:

  • -name 根据文件的basename进行匹配搜索
  • -path 根据文件的path+basename进行匹配搜索
  • -type 根据文件类型,f表示普通文件,d表示目录,l表示链接文件
  • -atime 根据文件的access time搜索
  • -mtime 根据文件的modify time搜索
  • -ctime 根据文件的change time搜索
  • -iname 根据文件的basename,不区分大小写
  • -size 根据文件的大小搜索
  • -perm 根据文件权限搜索

actions:

  • -print 默认动作,把find到的结果打印出来
  • -exec command; 执行command,比如把搜索到的文件删除掉
  • -ls 找到的文件以ls –dils的格式打印出来

示例

  1. 在/tmp目录下查找.log结尾的文件
    find /tmp/ -name “*.log“
  2. 在/tmp目录下查找目录中包含有ab,并且以9开头的文件
    find /tmp -path “*ab*/9*”
  3. 在/tmp目录下查找文件名带ab的目录文件
    find /tmp -type d -path “*ab*“
  4. 搜索/etc/目录下所有conf结尾的文件
    find /etc/ -maxdepth 1 -name “*conf”
  5. 搜索/etc/目录下所有以conf结尾的软链接文件。
    find /etc/ -maxdepth 1 -name "*conf" -type l
  6. 搜索/etc/目录下及子目录中所有以conf结尾的链接文件并把他们以ls -dils的格式打印出来
    find /etc/ -maxdepth 1 -name "*conf" -type l –ls
  7. 搜索/var目录下大雨1000M的文件
    find /var -size +100M

喜欢的话可以给一个推荐哟

posted @ 2021-09-25 15:45  海小海  阅读(844)  评论(0)    收藏  举报