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
Find all files with name containing string
提问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 locate
and find
commands 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.markdown
was 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 1
if 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, find
is a better and cleaner solution for your task.
但是,我再说一遍,find
对于您的任务来说,这是一个更好、更干净的解决方案。
回答by carlspring
Use grep as follows:
使用 grep 如下:
grep -R "touch" .
-R
means recurse. If you would rather not go into the subdirectories, then skip it.
-R
意味着递归。如果您不想进入子目录,请跳过它。
-i
means "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 -maxdepth
option should be before the -name
option, 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.C
or HellO.C
. To match is case insensitive pass the -iname
option as follows:
但是,它不会匹配HELLO.C
或HellO.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 f
option 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 -iname
works 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 grep
command:
将-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 bash
with 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 bar
in 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 zsh
and bash
here. While bash
will traverse soft-links to directories, zsh
will not. For this you have to use the glob ***/
in zsh
.
请注意,此处zsh
和bash
此处之间存在细微差别。虽然bash
会遍历目录的软链接,但zsh
不会。为此,您必须***/
在zsh
.
回答by Shahid
find / -exec grep -lR "{test-string}" {} \;