简体   繁体   English

如何在Linux中使用grep查找包含特定命令的所有文件?

[英]How to use grep in linux to find all files contain a specific command?

I'm trying to print the name of every C program in a specified directory tree that contains a "goto" command. 我正在尝试在包含“ goto”命令的指定目录树中打印每个C程序的名称。

In other words, it must print the name of every file that contains "goto" as a word. 换句话说,它必须将每个包含“ goto”的文件的名称打印出来。 Here is what I do: 这是我的工作:

for fullname in `grep -r -l "\<goto\>"./*.c`;
do
  echo `basename $fullname`
done

but on running this I only get 但是在运行这个我只会

"command not found". “找不到相关命令”。

Assuming that you want to also search nested directories this should work 假设您还想搜索嵌套目录,则该方法有效

for fullname in $(grep -r -l "goto" . | grep ".*\.c$"); do
    echo $(basename $fullname)
done

I know this is answered but the following will work for non-nested-directories. 我知道已回答,但以下内容适用于非嵌套目录。

grep "regex" /path/*

which shows 这表现了

t.txt:Loren ip sum let etum REGEX
v.c:do_regex_match("regex","bleh");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM