[英]How do I list the files having particular strings from group of directories in bash?
I want to list files having EXACT strings like "hello", "how" and "todo" from a directory (which is having multiple directories). 我想从目录(具有多个目录)中列出具有“ hello”,“ how”和“ todo”之类的确切字符串的文件。 Also I want to list c(.c) and cpp (.cpp) files only. 我也只想列出c(.c)和cpp(.cpp)文件。 I have tried with grep -R
( grep -R "hello" /home
) but not satisfied. 我曾尝试使用grep -R
( grep -R "hello" /home
),但不满意。 Please help me to enhance my grep -R
command or any alternate way. 请帮助我增强我的grep -R
命令或其他替代方法。 Thanks in advance. 提前致谢。
if you want to find files, a good start is usually to use find
. 如果要查找文件,通常最好使用find
。
if you want to find all .cpp
and .-c
files that contain the strings "hello", "how" or "todo" in their content, use something like: 如果要查找在其内容中包含字符串“ hello”,“ how”或“ todo”的所有.cpp
和.-c
文件,请使用类似以下内容的方法:
find /home \( -name "*.c" -or -name "*.cpp" \) \
-exec egrep -l "(hello|how|todo)" \{\} \;
if instead you want to find all .cpp
and .-c
files that contain the strings "hello", "how" or "todo" in their filenames, use something like: 相反,如果您要查找其文件名中包含字符串“ hello”,“ how”或“ todo”的所有.cpp
和.-c
文件,请使用以下命令:
find /home \
\( \( -name "*.c" -or -name "*.cpp" \) \
-and \
\( -name "*hello*" -or -name "*how*" -or -name "*todo*" \) \
\)
there is a bit of quoting (using \\
) involved, as ()
, {}
and ;
涉及到一些引号(使用\\
),例如()
, {}
和;
are considered special characters by the shell... 被shell视为特殊字符...
In fact grep itself would be fine for this. 实际上,grep本身对此很好。
However I would strongly suggest ack-grep . 但是,我强烈建议ack-grep 。 It is a good alternative to grep which just suit your need. 这是grep的一个很好的选择,它可以满足您的需求。
With ack-grep it is just as simple as 使用ack-grep就像
ack-grep --cc --cpp "(hello|how|todo)"
You can try the followings: 您可以尝试以下方法:
grep -rn --include={*.c,*.cpp} yourdirectory -e ^h[a-z]*
This will search through all the files which have .c and .cpp extensions and finds patterns starts with h (you need to prepare you own to meet your need) from your specified directory. 这将搜索所有扩展名为.c和.cpp的文件,并从指定目录中找到以h开头的模式(您需要准备自己的文件才能满足需要)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.