如何使用grep在Linux上显示文件名?

本文翻译自:How can I use grep to show just filenames on Linux?

How can I use grep to show just file-names (no in-line matches) on Linux? 如何在Linux上使用grep来显示文件名(没有内联匹配)?

I am usually using something like: 我通常使用类似的东西:

find . -iname "*php" -exec grep -H myString {} \;

How can I just get the file-names (with paths), but without the matches? 我怎样才能获得文件名(带路径),但没有匹配? Do I have to use xargs ? 我必须使用xargs吗? I didn't see a way to do this on my grep man page. 我没有在grep手册页上看到这样做的方法。


#1楼

参考:https://stackoom.com/question/Rqoc/如何使用grep在Linux上显示文件名


#2楼

For a simple file search you could use grep's -l and -r options: 对于简单的文件搜索,您可以使用grep的-l-r选项:

grep -rl "mystring"

All the search is done by grep. 所有的搜索都是由grep完成的。 Of course, if you need to select files on some other parameter, find is the correct solution: 当然,如果您需要在其他参数上选择文件,find是正确的解决方案:

find . -iname "*.php" -execdir grep -l "mystring" {} +

The execdir option builds each grep command per each directory, and concatenates filenames into only one command ( + ). execdir选项为每个目录构建每个grep命令,并将文件名连接到一个命令( + )。


#3楼

From the grep(1) man page: grep(1)手册页:

  -l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.) 

#4楼

The standard option grep -l (that is a lowercase L) could do this. 标准选项grep -l (即小写L)可以执行此操作。

From the Unix standard : Unix标准

-l
    (The letter ell.) Write only the names of files containing selected
    lines to standard output. Pathnames are written once per file searched.
    If the standard input is searched, a pathname of (standard input) will
    be written, in the POSIX locale. In other locales, standard input may be
    replaced by something more appropriate in those locales.

You also do not need -H in this case. 在这种情况下,您也不需要-H

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Linux 中,可以使用 `grep` 命令进行模糊查询文件名。 例如,要查找当前目录中以 ".txt" 结尾的文件,可以使用以下命令: ``` grep -r "\.txt$" . ``` -r 选项表示递归搜索当前目录下的所有子目录。 "\.txt$" 是模式,用来匹配文件名。 "." 表示当前目录。 如果想要在指定目录下查找文件名,可以将”.”替换成目录路径。 ### 回答2: grepLinux系统中一个非常强大的模糊查询命令,它可以在文件中查找包含某个特定字符串的行,并将符合条件的行输出。 例如,我们想要查找某个文件夹中包含特定字符串的文件,我们可以使用如下命令: grep "特定字符串" 文件夹名/* 在上述命令中,我们使用"*"通配符表示查找整个文件夹中的所有文件,并通过管道符"|“将其输出到下一个命令中进行处理。 除此之外,grep还支持使用正则表达式进行高级的模糊查询,并支持在多个文件中查找并列出符合条件的行。 如果我们只想列出包含特定字符串的文件名而不是文件内容,可以使用grep的-l选项,如下所示: grep -l "特定字符串" 文件夹名/* 总之,grepLinux系统中非常常用且强大的模糊查询命令之一,其功能丰富,可为用户提供便捷的文件搜索和处理功能。 ### 回答3: 在Linux系统中,grep是一种强大的文本搜索工具,可以用于模糊查询文件名grep命令可以在一个或多个文件、目录或者标准输入数据中查找匹配的文本,并返回包含匹配文本行的信息。 使用grep进行模糊查询文件名需要指定要搜索的目录和模式,其中,模式支持通配符和正则表达式。通配符包括*(匹配任意字符)和?(匹配一个字符)。正则表达式是一种更为高级的匹配模式,它包括元字符和特殊字符,如^、$、[a-z]等。 例如,要查找所有以“.txt”结尾的文件,可以使用以下命令: grep -r ".txt$" /path/to/directory 其中,-r表示递归搜索目录,.txt$表示以“.txt”结尾的文件名,/path/to/directory表示要搜索的目录。 如果要查找所有包含“linux”的文件名,可以使用以下命令: grep -r "linux" /path/to/directory 其中,"linux"表示要查找的字符串。 除了以上两种方式,还可以使用其他的模式进行模糊查询。通过组合不同的元字符和特殊字符,可以实现更灵活的查询,例如查找以a或b结尾的文件名grep -r "a$|b$" /path/to/directory 在使用grep进行模糊查询文件名时,有一些需要注意的地方。首先,需要确定要搜索的目录范围,不要将搜索范围设置得过大,否则会增加搜索的时间和系统负担。另外,需要谨慎使用正则表达式,因为有些特殊字符可能会被系统解释为其他命令,导致不必要的麻烦。 总之,grep命令是一种非常实用的工具,可以在Linux系统中进行快速的模糊查询文件名操作,提高工作效率。在实际使用中,需要结合具体的需求和场景,选择合适的搜索模式和参数,才能达到最佳的查询效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值