Linux find 常用命令
(2012-10-24 11:32:52)
标签:
linux
shell
说明:
以下内容,若标注在 [ ] 中括号内的均可省略,除非做特别说明。
但严格来说,若缺少 [ ] 中括号内的内容,在命令呈现的语义和返回结果上,偶尔会有些许差异。
正文:
0、使用说明
man find
找到所有关于 find 命令的说明
1、查找目录下的内容
1.1 所有文件
find /full/path -type f
1.2 所有文件夹
find /full/path -type d
1.3 所有 .php 文件
find /full/path [-type f] -name "*.php"
1.4 所有 .log 和 .txt 后续文件(多重选择,注意:括号内两边均带空格)
find /full/path [-type f] \( -name "*.log" -o -name "*.txt"
\)
或者 find /full/path [-type f] -regex ".*\.\(txt\|sh\)"
1.5 所有不是 .log 和 .txt 后续的文件(否定参数)
find /full/path [-type f] -not \( -name "*.log" -o -name "*.txt"
\)
或者 find /full/path [-type f] ! \( -name "*.log" -o -name "*.txt"
\)
1.6 查找大于 10KB 的日志文件
find /full/path [-type f] -size +10k
除k之外,还可以用其他文件大小单元
b—块(512字节)
c—字节
w—字(2字节)
k—千字节
M—兆字节
G—吉字节
1.7 查找 24 小时内改动过的文件
find /full/path [-type f] -ctime -1
1.8 修改时间为7天以前的文件
find /full/path [-type f] -mtime +7
1.9 查找当前目录下所有的隐藏(普遍)文件(可以用 -iregex 来去除大小写敏感)
find /full/path [-type f] -regex ".*\/\.[^/]*"
2.0 查找空文件(夹)
find /full/path -empty
2.1 查找当前目录中以及一级子目录中的 php 文件
find /full/path [-type f] -name "*.php" -maxdepth 2
2.2 查找目录下除report子目录以外的所有文件
find /full/path -path "*report" -prune -o -print
2.3 查找权限是 777 的文件
find /full/path [-type f] -perm 777
2.4 输出当前目录下所有 txt 文件的内容
find /full/path [-type f] -name "*.txt" -exec cat {} \;
或者 find /full/path [-type f] -name "*.txt" | xargs -i cat {}
在这个命令中,{}是一个特殊的字符串,与 -exec 选项结合使用。
对于每一个匹配的文件,{}会被替换成相应的文件名。
2.5 只搜索最小深度为3,最大尝试为5的所有文件夹 ( -maxdepth参数需要放在最前面 )
find /full/path -mindepth 3 -maxdepth 5 -type d
2.6 删除匹配的文件
find /full/path -type f -name "*.bak" -delete
分享:
喜欢
0
赠金笔
加载中,请稍候......
评论加载中,请稍候...
发评论
登录名: 密码: 找回密码 注册记住登录状态
昵 称:
评论并转载此博文
发评论
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。