Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 查找名称包含字符串的所有文件_Linux_Unix_Command Line_Locate - Fatal编程技术网

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

Linux 查找名称包含字符串的所有文件,linux,unix,command-line,locate,Linux,Unix,Command Line,Locate,我一直在搜索一个命令,该命令将从当前目录返回文件,文件名中包含一个字符串。我见过locate和find命令,它们可以查找以开头的文件*或以*.jpg结尾的文件 如何返回文件名中包含字符串的文件列表 例如,如果2012-06-04-touch-multiple-files-in-linux.markdown是当前目录中的文件 如何返回此文件以及包含字符串touch的其他文件?使用命令,如find'/touch/'使用grep,如下所示: grep -R "touch" . find $HOME

我一直在搜索一个命令,该命令将从当前目录返回文件,文件名中包含一个字符串。我见过
locate
find
命令,它们可以查找以
开头的文件*
或以
*.jpg
结尾的文件

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

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

如何返回此文件以及包含字符串
touch
的其他文件?使用命令,如
find'/touch/'
使用grep,如下所示:

grep -R "touch" .
find $HOME -iname "hello.c" -print
-R
表示递归。如果您不想进入子目录,请跳过它

-i
表示“忽略案例”。您可能会发现这也值得一试。

使用
find

find-maxdepth 1-名称“*字符串*”-打印

它将查找当前目录中包含“字符串”的所有文件(如果希望递归,请删除
maxdepth 1
),并将其打印在屏幕上

如果要避免文件包含“:”,可以键入:

find-maxdepth 1-名称“*字符串*”-名称“*:*”-打印

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

ls| grep touch


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

如果字符串位于名称的开头,您可以这样做

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

-maxdepth
选项应位于
-name
选项之前,如下所示

find . -maxdepth 1 -name "string" -print
这将在整个
$HOME
(即
/HOME/username/
)系统中搜索任何名为“hello.c”的文件,并显示其路径名:

/Users/user/Downloads/hello.c
/Users/user/hello.c
但是,它将不匹配
HELLO.C
HELLO.C
。要匹配不区分大小写,请按如下方式传递
-iname
选项:

grep -R "touch" .
find $HOME -iname "hello.c" -print
样本输出:

/Users/user/Downloads/hello.c
/Users/user/Downloads/Y/Hello.C
/Users/user/Downloads/Z/HELLO.c
/Users/user/hello.c
/Users/user/Downloads/Z/HELLO.C
/Users/user/Downloads/Z/HEllO.c
/Users/user/Downloads/hello.c
/Users/user/hello.c
传递
-type f
选项以仅搜索文件:

find /dir/to/search -type f -iname "fooBar.conf.sample" -print
find $HOME -type f -iname "fooBar.conf.sample" -print
-iname
在GNU或BSD(包括OS X)版本的find命令上工作。如果您的find命令版本不支持
-iname
,请使用
grep
命令尝试以下语法:

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

find $HOME -name '[hH][eE][lL][lL][oO].[cC]' -print
样本输出:

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

已经提供的许多解决方案的替代方案是使用glob
**
。当您使用带有选项
globstar
shopt-s globstar
)的
bash
)或使用
zsh
时,您只需使用glob
**

**/bar
递归目录搜索名为
bar
的文件(可能包括当前目录中的文件
bar
)。注意,这不能与同一路径段内的其他形式的全球化相结合;在这种情况下,
*
操作符将恢复其通常的效果


注意这里的
zsh
bash
之间有细微的区别。虽然
bash
将遍历指向目录的软链接,但
zsh
不会。为此,您必须在
zsh

中使用glob
***/
。我注意到一些文件内容遵循
。还有什么可以隐瞒的吗?也许使用一个选项?试试:
grep-R“touch”cut-d“:”-f2
,似乎只生成文件的内容。你基本上回答了我的问题,但我可以试着做一些挖掘来保留内容。啊。。。你只需要文件名?运行:
grep-R“touch”切-d:“-f 1
(很抱歉误读了您的意思)。谢谢@carlspring这很有趣
grep
要么返回包含
touch
内容和文件名的文件,要么返回包含
touch
内容的文件,我还不确定是哪种情况。在返回的文件列表中,一半在标题中包含
touch
,另一半在正文中包含
touch
,而不是标题。刚刚意识到这一点。@Dru,如果你想让它“更短”,你可以避免打印,因为这是默认行为,而这是默认文件夹,它会检查。太棒了。我看到我自己经常使用这个。我将接受您的
-print
删除建议,将其设置为命令,并尝试将
*string*
作为命令行参数传入。
查找-名称“*string*”
也很有用。删除
会在我这端抛出一个错误。再次感谢@Zagorax。只是一个观察,上面的命令抱怨了
-maxdepth
参数的位置,最好将其移到
-name
之前,因为@Sunil Dias提到edi已经
找到了*.jpg-name“*from*”-print
,它适用于给定的目录。如何进行递归搜索?我试过
-maxdepth
compgen
不是适合这个指甲的锤子。这个很少使用的工具旨在列出可用的命令,因此,它列出当前目录中的文件(可能是脚本),既不能递归,也不能查看文件名的开头,也不能搜索文件内容,这似乎是对另一个问题的回答,但因为你没有提供任何解释,这对提问者或其他任何人来说都不明显。你必须在“字符串”之前和之后加上*。这个答案被标记为低质量。请解释您提议的更改的作用。
find / -exec grep -lR "{test-string}" {} \;