linux 在目录中查找内容为给定字符串的文件
linux find file in directory with given string in content
在Linux(命令行)中:
我需要找到位于 /users/tom/
或其任何子目录中的所有 Perl 文件(文件名以 .pl
或 .pm
结尾)字符串 ->get(
和字符串 #hyphenate
(位于不同行或同一行)。我只需要文件的名称(及其路径)。我不需要文件中找到字符串的行。
是否有可以执行此操作的命令?
我知道如何查找具有一个扩展名的文件:
find /users/tom -name "*.pl"
但是我很难找到具有两种不同扩展名之一的文件。 None 此命令有效:
find /users/tom -name "*.pl" -name "*.pm"
find /users/tom -name "*.pl|*.pm"
我的解决方法是一个接一个地做,但我想一定有更优雅的方法。
现在文件内容:
我知道如何使用 grep:
打印文件名和匹配行
grep * -e "->get(" -e "#hyphenate"
这列出了至少包含一个搜索字符串的所有文件。但我想要一个包含 all 搜索字符串的文件列表。
如何做到这一点? (在 Ubuntu/Linux 中形成命令行)
grep
可以用-r
递归搜索目录。要仅获取文件名而不是匹配行,请使用 -l
.
grep -rl -- '->get(\|#hyphenate' /users/tom | grep '\.p[lm]$'
或者,使用查找:
find /users/tom -name '*.p[lm]' -exec grep -l -- '->get(\|#hyphenate' {} +
更新
上面搜索->get(
或#hyphenate
,如果两个都想要,则要运行grep
两次:
find /users/tom -name '*.p[lm]' -exec grep -l -- '->get(' {} + \
| xargs grep -l '#hyphenate'
如果您的文件名包含空格,您可能需要为第一个 grep
指定 -Z
并为 xargs
指定 -0
。
grep -r PLACE_YOUR_STRING_HERE | cut -d ' ' -f 1 | grep '.p1\|.pm'
将字符串替换为您要查找的模式,然后 运行 转到您要查找的文件夹后执行命令。
find /usr/tom | egrep '*.pl| *.pm' | xargs cat | grep <PATTERN>
在Linux(命令行)中:
我需要找到位于 /users/tom/
或其任何子目录中的所有 Perl 文件(文件名以 .pl
或 .pm
结尾)字符串 ->get(
和字符串 #hyphenate
(位于不同行或同一行)。我只需要文件的名称(及其路径)。我不需要文件中找到字符串的行。
是否有可以执行此操作的命令?
我知道如何查找具有一个扩展名的文件:
find /users/tom -name "*.pl"
但是我很难找到具有两种不同扩展名之一的文件。 None 此命令有效:
find /users/tom -name "*.pl" -name "*.pm"
find /users/tom -name "*.pl|*.pm"
我的解决方法是一个接一个地做,但我想一定有更优雅的方法。
现在文件内容:
我知道如何使用 grep:
grep * -e "->get(" -e "#hyphenate"
这列出了至少包含一个搜索字符串的所有文件。但我想要一个包含 all 搜索字符串的文件列表。
如何做到这一点? (在 Ubuntu/Linux 中形成命令行)
grep
可以用-r
递归搜索目录。要仅获取文件名而不是匹配行,请使用 -l
.
grep -rl -- '->get(\|#hyphenate' /users/tom | grep '\.p[lm]$'
或者,使用查找:
find /users/tom -name '*.p[lm]' -exec grep -l -- '->get(\|#hyphenate' {} +
更新
上面搜索->get(
或#hyphenate
,如果两个都想要,则要运行grep
两次:
find /users/tom -name '*.p[lm]' -exec grep -l -- '->get(' {} + \
| xargs grep -l '#hyphenate'
如果您的文件名包含空格,您可能需要为第一个 grep
指定 -Z
并为 xargs
指定 -0
。
grep -r PLACE_YOUR_STRING_HERE | cut -d ' ' -f 1 | grep '.p1\|.pm'
将字符串替换为您要查找的模式,然后 运行 转到您要查找的文件夹后执行命令。
find /usr/tom | egrep '*.pl| *.pm' | xargs cat | grep <PATTERN>