提问人:wpiri 提问时间:1/1/2010 最后编辑:Geoffrey Halewpiri 更新时间:10/11/2023 访问量:2195118
如何递归地gresing所有目录和子目录?
How do I recursively grep all directories and subdirectories?
答:
grep -r "texthere" .
第一个参数表示要搜索的正则表达式,而第二个参数表示应搜索的目录。在本例中,表示当前目录。.
注意:这适用于 GNU grep,在某些平台上,如 Solaris,您必须专门使用 GNU grep,而不是传统实现。对于 Solaris,这是命令。ggrep
评论
AIX 5.3
也:
find ./ -type f -print0 | xargs -0 grep "foo"
但这是一个更好的答案。grep -r
评论
find . -type f -exec grep "foo" '{}' \;
find ./ -type f -print0 | xargs -0 grep "foo"
如果您知道所需文件的扩展名或模式,另一种方法是使用选项:--include
grep -r --include "*.txt" texthere .
您还可以使用 提及要排除的文件。--exclude
银
如果您经常搜索代码,Ag(The Silver Searcher)是 grep 的更快替代品,grep 是为搜索代码而定制的。例如,它默认是递归的,并自动忽略 中列出的文件和目录,因此您不必继续传递同样繁琐的排除选项来 grep 或 find。.gitignore
评论
=
grep
--include "*.txt" --include "*.TXT"
只是文件名也很有用
grep -r -l "foo" .
我现在总是使用(即使在带有 GoW 的 Windows 上 -- Windows 上的 Gnu):
grep --include="*.xxx" -nRHI "my Text to grep" *
(正如 kronen 在评论中指出的那样,您可以添加 void 权限被拒绝的输出)2>/dev/null
这包括以下选项:
--include=PATTERN
在仅搜索文件匹配的目录中递归。
PATTERN
-n, --line-number
在每行输出前加上其输入文件中的行号。
(注意:phuclv 在评论中补充说 -n
会大大降低性能,因此您可能想跳过该选项)
-R, -r, --recursive
以递归方式读取每个目录下的所有文件;这等同于选项。
-d recurse
-H, --with-filename
打印每个匹配项的文件名。
-I
处理二进制文件,就好像它不包含匹配的数据一样;
这等同于选项。--binary-files=without-match
如果我想要不区分大小写的结果,我可以添加“”()。i
-nRHIi
我可以得到:
/home/vonc/gitpoc/passenger/gitlist/github #grep --include="*.php" -nRHI "hidden" *
src/GitList/Application.php:43: 'git.hidden' => $config->get('git', 'hidden') ? $config->get('git', 'hidden') : array(),
src/GitList/Provider/GitServiceProvider.php:21: $options['hidden'] = $app['git.hidden'];
tests/InterfaceTest.php:32: $options['hidden'] = array(self::$tmpdir . '/hiddenrepo');
vendor/klaussilveira/gitter/lib/Gitter/Client.php:20: protected $hidden;
vendor/klaussilveira/gitter/lib/Gitter/Client.php:170: * Get hidden repository list
vendor/klaussilveira/gitter/lib/Gitter/Client.php:176: return $this->hidden;
...
评论
-R
*
.
.
.git/
)
在 POSIX 系统中,您找不到参数 for,并且不会运行,但如果您使用命令,它将:-r
grep
grep -rn "stuff" .
find
find . -type f -exec grep -n "stuff" {} \; -print
同意 和 。Solaris
HP-UX
评论
-exec
{}
find
-exec
;
-print
find
这应该有效:
grep -R "texthere" *
请注意,当 find 匹配的文件太多时,各种解决方案将遇到“参数列表到长”错误。find . -type f | xargs grep whatever
最好的办法是,但如果不可用,请改用。grep -r
find . -type f -exec grep -H whatever {} \;
评论
xargs
find . -type f | xargs -L 100 grep whatever
xargs
是标准化的,具有开箱即用的此行为。“xargs
实用程序应限制命令行长度,以便在调用命令行时,组合参数和环境列表......不得超过 {ARG_MAX}-2048 字节。
AG 是我现在最喜欢的方式 github.com/ggreer/the_silver_searcher。它基本上与 ack 相同,但进行了更多优化。
这是一个简短的基准。我在每次测试前清除缓存(参见 https://askubuntu.com/questions/155768/how-do-i-clean-or-disable-the-memory-cache )
ryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time grep -r "hey ya" .
real 0m9.458s
user 0m0.368s
sys 0m3.788s
ryan@3G08:$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time ack-grep "hey ya" .
real 0m6.296s
user 0m0.716s
sys 0m1.056s
ryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time ag "hey ya" .
real 0m5.641s
user 0m0.356s
sys 0m3.444s
ryan@3G08$ time ag "hey ya" . #test without first clearing cache
real 0m0.154s
user 0m0.224s
sys 0m0.172s
在我的 IBM AIX Server(操作系统版本:AIX 5.2)中,使用:
find ./ -type f -print -exec grep -n -i "stringYouWannaFind" {} \;
这将打印出文件中的路径/文件名和相对行号,如下所示:
./inc/xxxx_x.h
2865: /** 描述 : stringYouWannaFind */
无论如何,它对我有用:)
只是为了好玩,如果@christangrant答案太多而无法输入,可以快速而肮脏地搜索 *.txt 个文件 :-)
grep -r texthere .|grep .txt
如果要从目录结构中查找所有文件中的特定内容,则可以使用,因为它更清楚您正在做什么:find
find -type f -exec grep -l "texthere" {} +
请注意,(L 的小写字母)显示包含文本的文件的名称。如果您想要打印匹配项本身,请将其删除。或者用于将文件与匹配项一起获取。总而言之,其他替代方案是:-l
-H
find -type f -exec grep -Hn "texthere" {} +
其中打印行号。-n
评论
find
xargs
+
\;
-exec
以递归方式查找包含以下特定用途的命令的名称
为:files
path
string
UNIX
find . | xargs grep "searched-string"
为:Linux
grep -r "searched-string" .
在服务器上查找文件UNIX
find . -type f -name file_name
在 LINUX 服务器上查找文件
find . -name file_name
grep -r "texthere" .
(期末通知期)
(^信用:https://stackoverflow.com/a/1987928/1438029)
澄清:
grep -r "texthere" /
(递归 grep 所有目录和子目录)
grep -r "texthere" .
(递归地 grep 这些目录和子目录)
grep 递归
grep [options] PATTERN [FILE...]
[选项]
-R, -r, --recursive
以递归方式读取每个目录下的所有文件。
这等效于 or 选项。
-d recurse
--directories=recurse
grep 帮助
$ grep --help
$ grep --help |grep recursive
-r, --recursive like --directories=recurse
-R, --dereference-recursive
选择
ack
(http://beyondgrep.com/)
ag
(http://github.com/ggreer/the_silver_searcher)
下面是用于递归搜索 on 和 environment 的命令。String
Unix
Linux
对于命令是:UNIX
find . -name "string to be searched" -exec grep "text" "{}" \;
对于命令是:Linux
grep -r "string to be searched" .
评论
find
-exec
-print0 | xargs -0
这是我当前机器上适用于我的情况(Windows 7 上的 git bash):
find ./ -type f -iname "*.cs" -print0 | xargs -0 grep "content pattern"
我总是忘记带有空格的路径的 -print0 和 -0。
编辑:我的首选工具现在是ripgrep:https://github.com/BurntSushi/ripgrep/releases。它的速度非常快,并且具有更好的默认值(例如默认的递归)。与我的原始答案相同的示例,但使用 ripgrep:rg -g "*.cs" "content pattern"
如果您只想关注实际目录,而不是符号链接,
grep -r "thingToBeFound" directory
如果你想遵循符号链接和实际目录(注意无限递归),
grep -R "thing to be found" directory
由于您尝试以递归方式进行 grep,因此以下选项也可能对您有用:
-H: outputs the filename with the line
-n: outputs the line number in the file
因此,如果您想在当前目录或任何子目录中找到包含 Darth Vader 的所有文件并捕获文件名和行号,但不希望递归遵循符号链接,则该命令将是
grep -rnH "Darth Vader" .
如果您想在目录中找到所有提及的单词 cat
/home/adam/Desktop/TomAndJerry
并且您当前位于目录中
/home/adam/Desktop/WorldDominationPlot
并且您想要捕获字符串“cats”的任何实例的文件名而不是行号,并且您希望递归遵循符号链接(如果找到它们),您可以运行以下任一命令
grep -RH "cats" ../TomAndJerry #relative directory
grep -RH "cats" /home/adam/Desktop/TomAndJerry #absolute directory
源:
运行“grep --help”
符号链接的简短介绍,对于任何阅读此答案并对我引用它们感到困惑的人:https://www.nixtutor.com/freebsd/understanding-symbolic-links/
评论
我想这就是你想写的
grep myText $(find .)
如果您想找到 grep 命中的文件,这可能是其他有用的东西
grep myText $(find .) | cut -d : -f 1 | sort | uniq
评论
下面是一个递归函数(使用 bash 和 sh 进行轻微测试),该函数遍历给定文件夹的所有子文件夹 ($1) 并使用搜索给定文件中的给定字符串 ($3) ($2):grep
$ cat script.sh
#!/bin/sh
cd "$1"
loop () {
for i in *
do
if [ -d "$i" ]
then
# echo entering "$i"
cd "$i"
loop "$1" "$2"
fi
done
if [ -f "$1" ]
then
grep -l "$2" "$PWD/$1"
fi
cd ..
}
loop "$2" "$3"
运行它和示例输出:
$ sh script start_folder filename search_string
/home/james/start_folder/dir2/filename
有关可用标志的列表:
grep --help
返回当前目录中正则表达式 texthere 的所有匹配项,并具有相应的行号:
grep -rn "texthere" .
返回 texthere 的所有匹配项,从根目录开始,具有相应的行号并忽略大小写:
grep -rni "texthere" /
此处使用的标志:
-r
递归的-n
打印带输出的行号-i
忽略大小写
在2018年,您想使用OR因为它们比替代品快得多。ripgrep
the-silver-searcher
下面是一个包含 336 个一级子目录的目录:
% find . -maxdepth 1 -type d | wc -l
336
% time rg -w aggs -g '*.py'
...
rg -w aggs -g '*.py' 1.24s user 2.23s system 283% cpu 1.222 total
% time ag -w aggs -G '.*py$'
...
ag -w aggs -G '.*py$' 2.71s user 1.55s system 116% cpu 3.651 total
% time find ./ -type f -name '*.py' | xargs grep -w aggs
...
find ./ -type f -name '*.py' 1.34s user 5.68s system 32% cpu 21.329 total
xargs grep -w aggs 6.65s user 0.49s system 32% cpu 22.164 total
在 OSX 上,这将安装: 。这将安装: 。ripgrep
brew install ripgrep
silver-searcher
brew install the_silver_searcher
评论
rg
rg
rg foo
find . | xargs grep foo
find . -print0 | xargs -0 grep foo
find . -type f -exec grep 'regex' {} +
ctags
etags
通配 **
使用有效,但可能会矫枉过正,尤其是在大文件夹中。grep -r
为了更实际的用法,这里是使用通配语法()的语法:**
grep "texthere" **/*.txt
它仅使用具有模式选择模式的特定文件。它适用于受支持的 shell,例如 Bash +4 或 zsh。
要激活此功能,请运行:。shopt -s globstar
Смотритетакже: 如何在 Linux 上找到包含特定文本的所有文件?
git grep
对于 Git 版本控制下的项目,请使用:
git grep "pattern"
这要快得多。
ripgrep
对于较大的项目,最快的 grepping 工具是 ripgrep
,它默认以递归方式 greps 文件:
rg "pattern" .
它建立在 Rust 的正则表达式引擎之上,该引擎使用有限自动机、SIMD 和激进的文字优化来使搜索变得非常快。在此处查看详细分析。
评论
把我的两分钱扔在这里。正如其他人已经提到的,grep -r 并非适用于每个平台。这听起来可能很傻,但我总是使用 git。
git grep "texthere"
即使目录没有暂存,我也只是暂存它并使用 git grep。
对于 .gz 文件,以递归方式扫描所有文件和目录 更改文件类型或放置 *
find . -name \*.gz -print0 | xargs -0 zgrep "STRING"
另一种语法,用于递归地在 Linux 系统上的所有文件中 grep 字符串
grep -irn "string"
命令的细分
-r, --recursive
表示在给定目录和子目录中查找指定字符串的搜索,以查找文件、二进制文件等中的特定字符串
recursive
-i, --ignore-case
忽略区分大小写,可用于添加倒置大小写字符串
-n, --line-number
打印找到的文件中指定字符串的行号
注意:这会将大量结果打印到控制台,因此您可能需要通过管道过滤输出并删除不太有趣的信息。它还搜索二进制程序,因此您可能需要过滤一些结果
从 grep 命令获取第一个匹配的文件,并获取所有文件不包含某些单词,但第二个 grep 的输入文件来自第一个 grep 命令的结果文件。
grep -l -r --include "*.js" "FIRSTWORD" * | xargs grep "SECONDwORD"
grep -l -r --include "*.js" "FIRSTWORD" * | xargs grep -L "SECONDwORD"
DC0FD654-37DF-4420-8BA5-6046A9DBE406
grep -l -r --include "*.js" "SEARCHWORD" * | awk -F'/' '{print $NF}' | xargs -I{} sh -c 'echo {}; grep -l -r --include "*.html" -w --include=*.js -e {} *; echo '''
5319778A-CEC2-444D-BCC4-53D33821联邦快递
grep "SEARCH_STRING" *.log | grep -e "http" -e "https" | awk '{print $NF}' | uniq
CE91D131-A5C2-4CC8-B836-1461FEEE6CDB
下面介绍如何修改命令以提取 messageName 的值:
grep -m 2 "In sendMessage:: " *LOGFILE.log | grep -o -e "messageName=[^,]*" | cut -d= -f2 | sort | uniq | tee >(echo "Number of unique values: $(wc -l)")
grep "In Message:: " *messaging.log | grep -o -e "messageName=[^,]*" | cut -d= -f2 | sort | uniq | while read -r messageName; do grep -m 1 "In sendMessage:: .*messageName=${messageName}" *logfile.log | head -n 1; done
我想在上面的文件上使用 run below grep 命令 2. 文件根据更新时间降序排序,与 .gz 格式不匹配
grep "org.springframework.batch.item.ItemStreamException: Failed to initialize the reader at" $(ls -lrth | grep -i opti | awk '{print $NF}')
grep -A 15 "request to URL : SEARCH" $(ls -lth | grep "common" | grep -v ".gz" | awk '{print $NF}')
命令创建从第一次出现到最后一次出现的新文件。
sed -n '/14 Jan 2023/,/14 Jan 2023/p' common.log > common_1day.log
今天修改的文件,
ls -lrth $(find . -type f -name "*.log" -newermt "$(date -R -d 'today 00:00')" -print)
grep "CID" $(find . -type f -name "*.log" -newermt "$(date -R -d 'today 00:00')" -print)
zgrep "SEARCH" $(find . -type f -newermt "$(date -R -d 'today 00:00')" -print)
ls -lrth $(find . -type f -name "*" -newermt "$(date -R -d 'today 00:00')" -print)
less +G $(find . -type f -name "*LOG_FILE.log" -newermt "$(date -R -d 'today 00:00')" -print)
grep Async $(find . -type f -name "*" -newermt "2023-04-14 00:00:00" ! -newermt "2023-04-16 00:00:00" -print)
查找命令
find . -type f -not -path "*/target/*" -name "log4j2.xml" -exec grep -H '<Async name="' {} \;
在 Solaris(可能还有其他旧的 Unix)上
ggrep -r “$yourtext” $directory
评论
grep -rin xlsx *.pl
在 Redhat Linux 上对我不起作用。我收到“不匹配”错误。