Linux 根据文件名模式和文件内容列出文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5914370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List file names based on a filename pattern and file content?
提问by zod
How can I use Grep command to search file name
based on a wild card "LMN2011*"
listing all files with this as beginning?
如何使用 Grep 命令file name
根据"LMN2011*"
列出所有以此开头的文件的通配符进行搜索?
I want to add another check on those file content.
我想对这些文件内容添加另一个检查。
If file content
has some thing like
如果file content
有类似的东西
LMN20113456
Can I use GREP
for this?
我可以用GREP
这个吗?
Grep -ls "LMN2011*" "LMN20113456"
What is the proper way to search the file names and its contents using shell commands?
使用 shell 命令搜索文件名及其内容的正确方法是什么?
采纳答案by jm666
Grep DOES NOT use "wildcards" for search – that's shell globbing, like *.jpg. Grep uses "regular expressions" for pattern matching. While in the shell '*' means "anything", in grep it means "match the previous item zero or more times".
Grep 不使用“通配符”进行搜索——这是 shell globbing,比如 *.jpg。Grep 使用“正则表达式”进行模式匹配。在 shell 中,'*' 的意思是“任何东西”,而在 grep 中,它的意思是“匹配前一项零次或多次”。
More information and examples here: http://www.regular-expressions.info/reference.html
更多信息和例子在这里:http: //www.regular-expressions.info/reference.html
To answer of your question - you can find files matching some pattern with grep:
要回答您的问题 - 您可以找到与 grep 匹配某种模式的文件:
find /somedir -type f -print | grep 'LMN2011' # that will show files whose names contain LMN2011
Then you can search their content (case insensitive):
然后你可以搜索他们的内容(不区分大小写):
find /somedir -type f -print | grep -i 'LMN2011' | xargs grep -i 'LMN20113456'
If the paths can contain spaces, you should use the "zero end" feature:
如果路径可以包含空格,则应使用“零端”功能:
find /somedir -type f -print0 | grep -iz 'LMN2011' | xargs -0 grep -i 'LMN20113456'
回答by Aoife
grep LMN20113456 LMN2011*
or if you want to search recursively through subdirectories:
或者,如果您想通过子目录递归搜索:
find . -type f -name 'LMN2011*' -exec grep LMN20113456 {} \;
回答by ugnelakys
find /folder -type f -mtime -90 | grep -E "(.txt|.php|.inc|.root|.gif)" | xargs ls -l > WWWlastActivity.log
find /folder -type f -mtime -90 | grep -E "(.txt|.php|.inc|.root|.gif)" | xargs ls -l > WWWlastActivity.log
回答by Jahanzeb Farooq
It can be done without find
as well by using grep's "--include"
option.
它也可以find
通过使用 grep 的"--include"
选项来完成。
grep man page says:
grep 手册页说:
--include=GLOB
Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).
So to do a recursive search for a string in a file matching a specific pattern, it will look something like this:
因此,要在匹配特定模式的文件中递归搜索字符串,它看起来像这样:
grep -r --include=<pattern> <string> <directory>
For example, to recursively search for string "mytarget" in all Makefiles:
例如,要在所有 Makefile 中递归搜索字符串“mytarget”:
grep -r --include="Makefile" "mytarget" ./
Or to search in all files starting with "Make" in filename:
或者在文件名中以“Make”开头的所有文件中搜索:
grep -r --include="Make*" "mytarget" ./
回答by ohho
Assume LMN2011*
files are inside /home/me
but skipping anything in /home/me/temp
or below:
假设LMN2011*
文件在里面,/home/me
但跳过里面/home/me/temp
或下面的任何内容:
find /home/me -name 'LMN2011*' -not -path "/home/me/temp/*" -print | xargs grep 'LMN20113456'