901. 【linux】find、xargs、grep 联合查找

2024-01-02  本文已影响0人  七镜

find命令可以根据指定的条件查找文件或目录,xargs命令可以将标准输入转换为命令的参数,grep命令可以根据指定的模式搜索文本。

在Linux中使用find、xargs、grep 3个工具找到某个文件中的指定字符,可以使用以下命令:

find PATH -type f | xargs grep PATTERN

其中:

例如,要查找当前目录下所有文件中包含“hello”字符的行,可以使用以下命令:

find . -type f | xargs grep -n hello

该命令将输出以下结果:

./test.txt:2 hello world

如果要查找递归查找当前目录及其子目录下所有文件中包含“hello”字符的行,可以使用以下命令:

find . -type f -exec grep -n hello {} \;

该命令将输出以下结果:

./test.txt:2 hello world
./test2.txt:3 hello world

还可以使用以下命令指定grep命令的其他选项,例如:

find . -type f | xargs grep -n -i hello

该命令将输出以下结果:

./test.txt:2 hello world
./test2.txt:3 hello world
上一篇 下一篇

猜你喜欢

热点阅读