find
是一个用来查找文件的shell命令。
find . -type f -size +1M
-size
用来指定要查找的文件的大小
find . -type f -mtime +7
-mtime
用来测试文件最后修改时间,+7
表示大于7天(24x7小时)
find . -depth -type d -empty -delete
其中,
-depth
指定深度优先以保证先删除子目录-empty
用来测试目录是否为空-delete
相当于-exec rm -rf {} \;
find $dir -name "*_GOLD" -prune -o -type f -name "*.tcl" -printf "%P\n"
-prune
跳过匹配目录及其子目录-o
逻辑或-printf
不打印命令行中的目录名find . -type f -exec grep -l "hello" {} \;
主要是利用grep
的"-l"选项。"-l"的作用是只列出匹配文件的文件名。
如果要同时看到文件名和第一个匹配行,也可以用 grep -H -m 1 "hello"
。
其实,grep
命令提供了选项-r
用于查找指定目录下的所有文件。我们也可以用下面的命令:
grep -r -m 1 "hello"