查找名称包含字符串的所有文件 [英] Find all files with name containing string
问题描述
我一直在寻找一个命令,该命令将从当前目录返回文件名中包含字符串的文件.我见过locate
和find
命令可以找到以first_word*
开头或以*.jpg
结尾的文件.
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
.
如何返回文件名中包含字符串的文件列表?
How can I return a list of files which contain a string in the filename?
例如,如果2012-06-04-touch-multiple-files-in-linux.markdown
是当前目录中的文件.
For example, if 2012-06-04-touch-multiple-files-in-linux.markdown
was a file in the current directory.
如何返回此文件和其他包含字符串touch
的文件?使用find '/touch/'
How could I return this file and others containing the string touch
? Using a command such as find '/touch/'
推荐答案
使用find
:
find . -maxdepth 1 -name "*string*" -print
它将在当前目录中找到所有包含字符串"的文件(如果要递归,请删除maxdepth 1
),并将其打印在屏幕上.
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.
如果要避免文件中包含:",则可以输入:
If you want to avoid file containing ':', you can type:
find . -maxdepth 1 -name "*string*" ! -name "*:*" -print
如果您想使用grep
(但我认为就不需要检查文件内容而言,则没有必要),您可以使用:
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:
ls | grep touch
但是,我重复一遍,find
是一种更好,更清洁的解决方案,可以满足您的任务.
But, I repeat, find
is a better and cleaner solution for your task.
这篇关于查找名称包含字符串的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!