grep可以使用-r
选项进行递归搜索。但是,我想知道grep是否能够递归搜索指定数目的子文件夹级别的查询字符串。例如,我有一个文件夹root
,其中有文件夹parent1, parent2, ..., parentN
。每个父文件夹都有普通的文本文件和名为child1, child2, ..., childM
的文件夹。我想从根目录运行grep并在父目录中搜索文件,而无需在子目录中查找。有没有简单的方法可以做到这一点?
最佳答案
您可以尝试以下方法:
grep :
--exclude=GLOB
Skip files whose base name matches GLOB (using
wildcard matching). A file-name glob can use *,
?, and [...] as wildcards, and \ to quote a wildcard
or backslash character literally.
--exclude-from=FILE
Skip files whose base name matches any of the file-name
globs read from FILE (using wildcard matching as
described under --exclude).
--exclude-dir=DIR
Exclude directories matching the pattern DIR from
recursive searches.
或使用此
find | xargs grep
通过查找,您可以定义级别
编辑
在Linux/unix世界中,将一个命令输出到另一个命令非常普遍。我敢打赌你每天都这样做。
echo "abc"|sed 's/a/x/'
find . -name "*.pyc" |xargs rm
awk 'blahblah' file | sort |head -n2
tree|grep 'foo'
mvn compile|ack 'error'
...
请注意,并非以上所有示例都是有效的。它们只是示例。
关于search - 递归使用grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15167854/