简体   繁体   English

如何在bash中的目录组中列出具有特定字符串的文件?

[英]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 -Rgrep -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的一个很好的选择,它可以满足您的需求。

You can find it here 你可以在这里找到

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.

相关问题 如何通过脚本列出Bash中的目录和文件? - How to list directories and files in a Bash by script? 如何在Linux中的目录和子目录中的特定单词之下找到特定单词的文件/(文件的pwd) - how to find the files/(pwd of file) which is having a particular word below a particular word in directories and sub directories in linux 如何使bash函数创建多个命名目录,然后按扩展名组织其包含的文件? - How do I make a bash function to create multiple named directories and then organize their included files by extension? 如何在忽略某些目录的同时找到超过特定大小的所有文件(使用bash脚本) - How do I find all files above a certain size while ignoring certain directories (using a bash script) 如何对文件列表进行排序并在bash中连接内容? - How do I sort a list of files and concatenate the contents in bash? 如何从MAKEFILE中的字符串列表中提取具有特定模式的字符串? - How can I extract strings with a particular pattern from a list of strings in MAKEFILE? 如何在 Bash 中对字母数字字符串进行排序 - How do I sort alphanumeric strings in Bash 如何在文件夹中没有特定扩展名的情况下搜索 bash 脚本文件? - How do I search for bash script files without having a specific extension within a folder? bash:检查目录中的多个文件是否包含列表中的字符串 - bash: check if multiple files in a directory contain strings from a list bash中的目录列表如何创建子目录列表 - How to create a list of subdirectories in a list of directories in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM