命令在linux环境中搜索字符串

时间:2016-05-06 18:36:43

标签: grep

我有unix目录路径。此目录包含5个子文件夹。 在每个子文件夹下,我有10个文件(类似)。此子文件夹表示每个站点或位置文件。

我想搜索每个子文件夹中存在的特定数字。

我目前正在使用grep命令,这对于在子文件夹中查找字符串很有用,但不是在所有子文件夹中

grep -l搜索字符串*。 这是为子文件夹执行。 我想使用一个命令搜索所有五个子文件夹。

2 个答案:

答案 0 :(得分:1)

using one single command - 为什么? UNIX建立在使用正确命令组合以获得最佳结果的基础上,为什么要做不同的事情呢?

在这种情况下,查找文件的UNIX工具名为find,而 G 的UNIX工具会自动搜索 R egular E 文件中的xpression和 P rint结果为grep因此,问题的正确方法是findgrep,例如:

find . -type f -print0 | xargs -0 grep -l regexp

答案 1 :(得分:1)

grep -r  

是你想要的

男人grep
/ - 递归

-r, --recursive
Read all files under each directory, recursively, following
symbolic links only if they are on  the  command  line.
Note that if no file operand is given, grep searches the working directory.
This is equivalent to the -d recurse option.