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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:39:11  来源:igfitidea点击:

Linux find file names with given string

linuxstringfind

提问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 grepcan 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 Rich Adams

Use the findcommand,

使用查找命令,

find . -type f -name "*John*"

回答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 findcommand will take long time because it scans real files in file system.

find命令需要很长时间,因为它扫描文件系统中的真实文件。

The quickest way is using locatecommand, which will give result immediately:

最快的方法是使用locate命令,它会立即给出结果:

locate "John"

If the command is not found, you need to install mlocatepackage and run updatedbcommand 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 treecommand in the directory you want to search for. -fshows the full file path and |is used to pipe the output of tree to grepto find the file containing the string filenamein the name.

这是一个非常简单的解决方案,使用tree您要搜索的目录中的命令。-f显示完整的文件路径,|并用于将树的输出通过管道传输grepfilename名称中包含字符串的文件。

tree -f | grep filename