Linux 查找名称包含字符串的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11328988/
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 13:43:02  来源:igfitidea点击:

Find all files with name containing string

linuxunixcommand-linelocate

提问by Dru

I have been searching for a command that will return files from the current directory which contain a string in the filename. I have seen locateand findcommands that can find files beginning with something first_word*or ending with something *.jpg.

我一直在寻找一个命令,该命令将从当前目录中返回文件名中包含字符串的文件。我已经看到locate,并find能找到的命令开始与一些文件first_word*或东西结束*.jpg

How can I return a list of files which contain a string in the filename?

如何返回文件名中包含字符串的文件列表?

For example, if 2012-06-04-touch-multiple-files-in-linux.markdownwas a file in the current directory.

例如,如果2012-06-04-touch-multiple-files-in-linux.markdown是当前目录中的文件。

How could I return this file and others containing the string touch? Using a command such as find '/touch/'

我怎么能返回这个文件和其他包含字符串的文件touch?使用命令,例如find '/touch/'

采纳答案by Zagorax

Use find:

使用find

find . -maxdepth 1 -name "*string*" -print

find . -maxdepth 1 -name "*string*" -print

It will find all files in the current directory (delete maxdepth 1if you want it recursive) containing "string" and will print it on the screen.

它将在当前目录中找到所有maxdepth 1包含“string”的文件(如果你想要递归,请删除)并将其打印在屏幕上。

If you want to avoid file containing ':', you can type:

如果你想避免包含':'的文件,你可以输入:

find . -maxdepth 1 -name "*string*" ! -name "*:*" -print

find . -maxdepth 1 -name "*string*" ! -name "*:*" -print

If you want to use grep(but I think it's not necessary as far as you don't want to check file content) you can use:

如果您想使用grep(但我认为只要您不想检查文件内容就没有必要),您可以使用:

ls | grep touch

ls | grep touch

But, I repeat, findis a better and cleaner solution for your task.

但是,我再说一遍,find对于您的任务来说,这是一个更好、更干净的解决方案。

回答by carlspring

Use grep as follows:

使用 grep 如下:

grep -R "touch" .

-Rmeans recurse. If you would rather not go into the subdirectories, then skip it.

-R意味着递归。如果您不想进入子目录,请跳过它。

-imeans "ignore case". You might find this worth a try as well.

-i意思是“忽略大小写”。您可能会发现这也值得一试。

回答by Steven Penny

If the string is at the beginning of the name, you can do this

如果字符串在名字的开头,你可以这样做

$ compgen -f .bash
.bashrc
.bash_profile
.bash_prompt

回答by Sunil Dias

The -maxdepthoption should be before the -nameoption, like below.,

-maxdepth选项应该是之前-name的选项,如下图所示,

find . -maxdepth 1 -name "string" -print

回答by shilovk

find $HOME -name "hello.c" -print

This will search the whole $HOME(i.e. /home/username/) system for any files named “hello.c” and display their pathnames:

这将在整个$HOME(即/home/username/)系统中搜索任何名为“hello.c”的文件并显示它们的路径名:

/Users/user/Downloads/hello.c
/Users/user/hello.c

However, it will not match HELLO.Cor HellO.C. To match is case insensitive pass the -inameoption as follows:

但是,它不会匹配HELLO.CHellO.C。要匹配不区分大小写,请-iname按如下方式传递选项:

find $HOME -iname "hello.c" -print

Sample outputs:

示例输出:

/Users/user/Downloads/hello.c
/Users/user/Downloads/Y/Hello.C
/Users/user/Downloads/Z/HELLO.c
/Users/user/hello.c

Pass the -type foption to only search for files:

传递-type f选项以仅搜索文件:

find /dir/to/search -type f -iname "fooBar.conf.sample" -print
find $HOME -type f -iname "fooBar.conf.sample" -print

The -inameworks either on GNU or BSD (including OS X) version find command. If your version of find command does not supports -iname, try the following syntax using grepcommand:

-iname在GNU或BSD(包括OS X)的版本find命令仍然可以正常工作。如果您的 find 命令版本不支持-iname,请使用grep命令尝试以下语法:

find $HOME | grep -i "hello.c"
find $HOME -name "*" -print | grep -i "hello.c"

OR try

或尝试

find $HOME -name '[hH][eE][lL][lL][oO].[cC]' -print

Sample outputs:

示例输出:

/Users/user/Downloads/Z/HELLO.C
/Users/user/Downloads/Z/HEllO.c
/Users/user/Downloads/hello.c
/Users/user/hello.c

回答by Saurabh kukade

grep -R "somestring" | cut -d ":" -f 1

回答by kvantour

An alternative to the many solutions already provided is making use of the glob **. When you use bashwith the option globstar(shopt -s globstar) or you make use of zsh, you can just use the glob **for this.

已经提供的许多解决方案的替代方案是使用 glob **。当您bash与选项globstar( shopt -s globstar) 一起使用或使用 时zsh,您可以仅使用 glob**来实现此目的。

**/bar

does a recursive directory search for files named bar(potentially including the file barin the current directory). Remark that this cannot be combined with other forms of globbing within the same path segment; in that case, the *operators revert to their usual effect.

对命名bar的文件bar进行递归目录搜索(可能包括当前目录中的文件)。请注意,这不能与同一路径段内的其他形式的通配符结合使用;在这种情况下,*运营商恢复其通常的效果。

Note that there is a subtle difference between zshand bashhere. While bashwill traverse soft-links to directories, zshwill not. For this you have to use the glob ***/in zsh.

请注意,此处zshbash此处之间存在细微差别。虽然bash会遍历目录的软链接,但zsh不会。为此,您必须***/zsh.

回答by Shahid

find / -exec grep -lR "{test-string}" {} \;