
find 命令是一个强大的搜索工具,可以搜索指定的目录及其子目录。要排除某些目录,可以使用 -path 参数结合 -not 参数。例如,要搜索当前目录下除 temp 和 backup 目录外的所有 .txt 文件,可以使用以下命令:
find . -path './temp' -prune -o -path './backup' -prune -o -name '*.txt' -print
grep 命令是一个强大的文本搜索工具,可以搜索文件内容。要排除某些目录,可以结合 find 命令使用 -exec grep 选项。例如,要搜索当前目录下除 temp 和 backup 目录外的所有 .txt 文件中包含 hello 字符串的行,可以使用以下命令:
find . -path './temp' -prune -o -path './backup' -prune -o -name '*.txt' -exec grep -H 'hello' {} \;
rg (ripgrep) 是一个快速且功能丰富的文本搜索工具,相比 grep 更加高效。要排除某些目录,可以使用 --ignore 参数。例如,要搜索当前目录下除 temp 和 backup 目录外的所有 .txt 文件中包含 hello 字符串的行,可以使用以下命令:
rg -g '*.txt' --ignore temp --ignore backup 'hello'
ls 命令可以列出指定目录下的文件和子目录。要排除某些目录,可以结合其他命令使用。例如,要列出当前目录下除 temp 和 backup 目录外的所有 .txt 文件,可以使用以下命令:
ls *.txt | grep -v 'temp\|backup'
以上是几种在 Linux 中搜索文件并排除某些目录的常用方法,希望对您有所帮助。