Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63

Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63

Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63

find 命令详解

文件与目录 · 在指定目录下查找文件 · 危险命令,请谨慎使用

语法

find [路径] [表达式] [动作]

常用选项

  • -name 按文件名匹配(支持通配符,记得加引号)
  • -iname 忽略大小写匹配名称
  • -type 按类型 f 文件 / d 目录 / l 链接
  • -mtime 按修改天数 +N 更早 / -N 之内
  • -size 按大小 +10M 大于 / -1k 小于
  • -exec 对每个结果执行命令,{} 占位,结尾 \; 或 +
  • -delete 直接删除匹配项(危险)
  • -maxdepth 限制递归深度

实用示例

  • find . -type f -name "*" | xargs grep "140.206.111.111"
  • find .
  • find /home -name "*.txt"
  • find /home -iname "*.txt"
  • find . -name "*.txt" -o -name "*.pdf"
  • find /usr/ -path "*local*"

使用提示

★ -delete 与 -exec rm 不可对未验证结果使用;先不加动作预览一遍

原文摘录

find 命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
参考:http://man.linuxde.net/find
# 当前目录搜索所有文件,文件内容 包含 “140.206.111.111” 的内容
find . -type f -name "*" | xargs grep "140.206.111.111"

# 列出当前目录及子目录下所有文件和文件夹
find .

# 在 /home 目录下查找以 .txt 结尾的文件名
find /home -name "*.txt"
# 同上,但忽略大小写
find /home -iname "*.txt"

# 当前目录及子目录下查找所有以 .txt 和 .pdf 结尾的文件
find . -name "*.txt" -o -name "*.pdf"

# 匹配文件路径或者文件
find /usr/ -path "*local*"

# 基于正则表达式匹配文件路径
find . -regex ".*\(\.txt\|\.pdf\)$"
# 同上,但忽略大小写
find . -iregex ".*\(\.txt\|\.pdf\)$"

# 找出 /home 下不是以 .txt 结尾的文件
find /home ! -name "*.txt"

相关命令

locate · grep · xargs

内容与示例整理自 dunwu/linux-tutorial,遵循 CC BY-SA 4.0;本站保留原作者署名与许可,命令仅供学习,执行前请核对目标环境。