一. grep
1.-r 选项表示递归搜索指定目录及其子目录
grep -r "methodName" frameworks/base/services/core/java/com/android/server/am
2.可使用 --include 选项指定搜索的文件类型。比如,要搜索 Java 文件中的方法:
grep -r --include="*.java" "methodName" frameworks/base
3.使用 grep 的 -w 选项进行精确单词匹配,避免搜到包含该方法名片段的其他内容。例如:
grep -r -w "methodName" frameworks/base
4.如果方法名有一定规律,可使用正则表达式来更精准地搜索。例如,搜索以 get 开头的方法:
grep -r -E "^.*get[A-Z].*\\(.*\\)" frameworks/base
5.
-w 表示只匹配完整的单词
-n 显示匹配行号
-r 递归搜索,包含子目录
./ 当前目录
--include= 定扩展名的文件中进行搜索
-i 忽略大小写
exclude-dir="test" 忽略文件夹
--exclude="a.txt" 忽略文件
-v 反向匹配 grep -v "error" log.txt 输出 log.txt 文件中所有不包含 "error" 的行
-x 整行匹配 grep -x "This is a test" test.txt test.txt 文件中存在整行为 "This is a test" 的行时,才会将其输出。
-c 统计匹配行数 grep -c "warning" system.log system.log 文件中包含 "warning" 的行数。而不是输出具体的匹配行内容。
-l 只输出文件名 grep -l "function" *.py 列出当前目录下所有包含 "function" 的 Python 文件的文件名
-E 扩展正则表达式 grep -E "[a-z]+@[a-z]+\.[a-z]+" emails.txt 在 emails.txt 文件中查找符合简单邮箱格式的字符串
-F grep -F ".com" urls.txt 在 urls.txt 文件中查找包含固定字符串 ".com" 的行
grep -wnri "keyword" --include="*.c" --include="*.cpp" --include="*.cc" --include="*.h" --include="*.hpp" --include="*.java"./
二.find
1.查找当前目录下所有扩展名为 .txt 的文件
find ./ -name "*.txt"
2.查找指定路径指定名称的文件
find /path/to/search -name "filename.txt"
3.不区分大小写查找文件名 -iname
find /path/to/search -iname "filename.txt"
4. 查找普通文件 -type f
find /path/to/search -type f
5.查找目录 -type d
find /path/to/search -type d
6.查找符号链接 -type l
find /path/to/search -type l
7.查找大于指定大小的文件 -size +10M
find /path/to/search -size +10M
8.查找小于指定大小的文件-size -500k
find /path/to/search -size -500k
9.查找最近 N 天内修改过的文件 -mtime -7
find /path/to/search -mtime -7
10.查找 N 天前修改过的文件 -mtime +30
find /path/to/search -mtime +30
11.表示查找权限为 644 的文件和目录。
find /path/to/search -perm 644
12.排除指定目录查找
find /path/to/search -path "/path/to/exclude" -prune -o -name "*.txt"
-path "/path/to/exclude":指定要排除的目录路径。
-prune:跳过该目录。
-o:逻辑或,表示要么跳过该目录,要么匹配 .txt 文件。
13.删除查找到的文件
find /path/to/search -name "*.bak" -exec rm {} \;
-exec:对查找到的文件执行指定的命令。
{}:表示查找到的文件路径。
\;:-exec 的结束标志。
14.批量重命名文件
将查找到的所有 .jpg 文件重命名为 .png 文件
find /path/to/search -name "*.jpg" -exec sh -c 'mv "{}" "${0%.jpg}.png"' sh {} \;