如何在Linux上查找包含特定文本的所有文件?
我试图找到一种方法来扫描我的整个Linux系统,查找包含特定文本字符串的所有文件。我只是想澄清一下,我在寻找文件中的文本,而不是文件名中的文本 当我查找如何执行此操作时,我两次遇到此解决方案:如何在Linux上查找包含特定文本的所有文件?,linux,text,grep,directory,find,Linux,Text,Grep,Directory,Find,我试图找到一种方法来扫描我的整个Linux系统,查找包含特定文本字符串的所有文件。我只是想澄清一下,我在寻找文件中的文本,而不是文件名中的文本 当我查找如何执行此操作时,我两次遇到此解决方案: find / -type f -exec grep -H 'text-to-find-here' {} \; 但是,它不起作用。它似乎显示了系统中的每个文件 这接近正确的方法吗?如果没有,我该怎么做?这种在文件中查找文本字符串的能力对于我正在进行的一些编程项目非常有用。您可以使用grep-ilR: gr
find / -type f -exec grep -H 'text-to-find-here' {} \;
但是,它不起作用。它似乎显示了系统中的每个文件
这接近正确的方法吗?如果没有,我该怎么做?这种在文件中查找文本字符串的能力对于我正在进行的一些编程项目非常有用。您可以使用
grep-ilR
:
grep -Ril "text-to-find-here" /
表示忽略案例(在您的案例中为可选)李>i
代表递归李>R
代表“显示文件名,而不是结果本身”l
表示从机器的根开始/
grep-rnw'/path/to/somewhere/'-e“模式”
或-r
是递归的-r
是行号,并且-n
代表匹配整个单词-w
(小写l)可以添加为仅提供匹配文件的文件名-l
是搜索过程中使用的模式-e
--exclude
,--include
,--exclude dir
标志还可用于高效搜索:
- 这将仅搜索扩展名为.c或.h的文件:
grep--include=\*.{c,h}-rnw'/path/to/somewhere/'-e“模式”
- 这将排除搜索以.o扩展名结尾的所有文件:
grep--exclude=\*.o-rnw'/path/to/somewhere/'-e“模式”
- 对于目录,可以使用
参数排除一个或多个目录。例如,这将排除dirs dir1/、dir2/以及所有匹配*.dst/:--exclude dir
grep--exclude dir={dir1,dir2,*.dst}-rnw'/path/to/somewhere/'-e“模式”
这对我来说非常有效,可以达到与你几乎相同的目的
有关更多选项,请查看您可以使用的man grep
。这就像源代码一样。您可以用它扫描整个文件系统
只要做:
ack 'text-to-find-here'
在您的根目录中
还可以使用、指定文件类型等
更新 我刚刚发现,它类似于ack,但比ack快3-5倍,甚至忽略了
.gitignore
文件中的模式。您可以使用以下方法:
grep -inr "Text" folder/to/be/searched/
包含给定文本的文件名列表
首先,我相信您使用了-H
而不是-l
。您还可以尝试在引号内添加文本,后跟{}\
find / -type f -exec grep -l "text-to-find-here" {} \;
例子
假设您正在目录中搜索包含特定文本“Apache许可证”的文件。它将显示与下面类似的结果(输出将根据目录内容而有所不同)
消除大小写敏感性
即使不使用“text”与“text”之类的大小写,也可以使用-i
开关忽略大小写。您可以阅读更多详细信息
希望这对你有所帮助。我写了一篇类似的文章。这就是我们应该如何使用这个脚本
./sniff.py path pattern_to_search [file_pattern]
第一个参数,path
,是我们将在其中递归搜索的目录。第二个参数,pattern\u to\u search
,是我们要在文件中搜索的正则表达式。我们使用re
库中定义的正则表达式格式。在这个脚本中,
也匹配换行符
第三个参数,file\u pattern
,是可选的。这是另一个作用于文件名的正则表达式。只考虑与此正则表达式匹配的文件
例如,如果我想搜索扩展名为py
包含Pool(
后跟wordadapter
)的Python文件,我会执行以下操作
./sniff.py . "Pool(.*?Adaptor" .*py
./Demos/snippets/cubeMeshSigNeur.py:146
./Demos/snippets/testSigNeur.py:259
./python/moose/multiscale/core/mumbl.py:206
./Demos/snippets/multiComptSigNeur.py:268
瞧,它会生成匹配文件的路径和找到匹配项的行号。如果找到多个匹配项,则每个行号都会附加到文件名中。要搜索字符串并仅输出带有搜索字符串的行,请执行以下操作:
for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
e、 g:
要显示包含搜索字符串的文件名,请执行以下操作:
for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;
e、 g:
以下是几个可用于搜索文件的命令列表
grep "text string to search” directory-path
grep [option] "text string to search” directory-path
grep -r "text string to search” directory-path
grep -r -H "text string to search” directory-path
egrep -R "word-1|word-2” directory-path
egrep -w -R "word-1|word-2” directory-path
您可以使用:
grep -r "string to be searched" /path/to/dir
r
代表递归,因此将在指定的路径及其子目录中搜索。这将告诉您文件名,并打印出文件中字符串出现的行
或类似于您正在尝试的命令(例如:)的命令,用于在所有javascript文件(*.js)中搜索:
这将打印文本出现的文件中的行,但不打印文件名
除此命令外,我们还可以编写以下命令:
grep-rn“要搜索的字符串”/path/to/directory/or/file
-r:递归搜索
n:将显示匹配的行号
grep
即使我们不寻找字符串也可以使用
简单地运行
grep -RIl "" .
将打印出所有文本文件的路径,即仅包含可打印字符的文本文件。如果您的
grep
不支持递归搜索,您可以将find
与xargs
组合使用:
find / -type f | xargs grep 'text-to-find-here'
我发现这比find-exec
的格式更容易记住
这将输出文件名和匹配行的内容,例如
/home/rob/file:text-to-find-here
您可能要添加到grep
的可选标志:
-不区分大小写的搜索-i
-仅输出找到匹配项的文件名-l
-仅输出匹配的行(而不是文件名)-h
find . -name '*.js' -exec grep -i 'string to search for' {} \; -print
grep -RIl "" .
find / -type f | xargs grep 'text-to-find-here'
/home/rob/file:text-to-find-here
find /path -type f -exec grep -l "string" {} \;
-type f specifies that it should proceed only files, not directories etc.
-exec grep specifies that for every found file, it should run grep command, passing its filename as an argument to it, by replacing {} with the filename
find . -name "*.txt" | xargs grep -i "text_pattern"
find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
find . -type f \( -name "*.pas" -o -name "*.dfm" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searchtext"
find / -type f -exec grep -H 'text-to-find-here' {} \;
find ~/ -type f -exec grep -H 'text-to-find-here' {} \;
find ./ -type f -exec grep -H 'text-to-find-here' {} \;
grep -r "class foo" .
grep "class foo" **/*.c
find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
rg "class foo" .
grep -e TEXT *.log | cut -d' ' --complement -s -f1
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
rg 'text-to-find-here' / -l
ag 'text-to-find-here' / -l
ack 'text-to-find-here' / -l
find ./ -name "file_pattern_name" -exec grep -r "pattern" {} \;
grep -insr "pattern" *
grep -rnw `pwd` -e "pattern"
ack -i search_string folder_path/*
sudo apt install silversearcher-ag
ag "Search query"
grep -c Your_Pattern *
alias ffind find / -type f | xargs grep
ffind 'text-to-find-here'
grep -rl 'pattern_to_find' /path/where/to/find
-r to recursively find a file / directory inside directories..
-l to list files matching the 'pattern'
grep -r 'pattern_to_find' /path/where/to/find
grep -r <text_fo_find> <directory>
grep -ir <text_to_find> <directory>