Linux 用给定的字符串查找文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13131048/
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
Linux find file names with given string
提问by JJ Beck
I'm on Ubuntu, and I'd like to find all files in the current directory and subdirectories whose name contains the string "John". I know that grep
can match the content in the files, but I have no idea how to use it with file names. Any help would be appreciated.
我在 Ubuntu 上,我想在当前目录和子目录中查找名称包含字符串“John”的所有文件。我知道grep
可以匹配文件中的内容,但我不知道如何将它与文件名一起使用。任何帮助,将不胜感激。
回答by tink
A correct answer has already been supplied, but for you to learn how to help yourself I thought I'd throw in something helpful in a different way; if you can sum up what you're trying to achieve in one word, there's a mighty fine help feature on Linux.
已经提供了正确的答案,但是为了让您学习如何帮助自己,我想我会以不同的方式提供一些有用的东西;如果你能用一句话概括你想要实现的目标,那么 Linux 上有一个非常好的帮助功能。
man -k <your search term>
What that does is to list all commands that have your search term in the short description. There's usually a pretty good chance that you will findwhat you're after. ;)
这样做是在简短描述中列出所有具有您的搜索词的命令。通常,您很有可能会找到您想要的东西。;)
That output can sometimes be somewhat overwhelming, and I'd recommend narrowing it down to the executables, rather than all available man-pages, like so:
该输出有时可能有点令人难以抗拒,我建议将其缩小到可执行文件,而不是所有可用的手册页,如下所示:
man -k find | egrep '\(1\)'
or, if you also want to look for commands that require higher privilege levels, like this:
或者,如果您还想查找需要更高权限级别的命令,如下所示:
man -k find | egrep '\([18]\)'
回答by Annu
use ack its simple.
just type ack <string to be searched>
使用 ack 很简单。只需输入ack <string to be searched>
回答by thucnguyen
The find
command will take long time because it scans real files in file system.
该find
命令需要很长时间,因为它扫描文件系统中的真实文件。
The quickest way is using locate
command, which will give result immediately:
最快的方法是使用locate
命令,它会立即给出结果:
locate "John"
If the command is not found, you need to install mlocate
package and run updatedb
command first to prepare the search database for the first time.
如果没有找到该命令,则需要先安装mlocate
包并运行updatedb
命令,以便第一次准备搜索数据库。
More detail here: https://medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab
更多细节在这里:https: //medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab
回答by caylus
This is a very simple solution using the tree
command in the directory you want to search for. -f
shows the full file path and |
is used to pipe the output of tree to grep
to find the file containing the string filename
in the name.
这是一个非常简单的解决方案,使用tree
您要搜索的目录中的命令。-f
显示完整的文件路径,|
并用于将树的输出通过管道传输grep
到filename
名称中包含字符串的文件。
tree -f | grep filename