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?
For example, if 2012-06-04-touch-multiple-files-in-linux.markdown
was a file in the current directory.
How could I return this file and others containing the string touch
? Using a command such as find '/touch/'
An alternative to the many solutions already provided is making use of the glob
**
. When you usebash
with the optionglobstar
(shopt -s globstar
) or you make use ofzsh
, you can just use the glob**
for this.does a recursive directory search for files named
bar
(potentially including the filebar
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.Note that there is a subtle difference between
zsh
andbash
here. Whilebash
will traverse soft-links to directories,zsh
will not. For this you have to use the glob***/
inzsh
.Use grep as follows:
-R
means recurse. If you would rather not go into the subdirectories, then skip it.-i
means "ignore case". You might find this worth a try as well.The
-maxdepth
option should be before the-name
option, like below.,If the string is at the beginning of the name, you can do this
This will search the whole
$HOME
(i.e./home/username/
) system for any files named “hello.c” and display their pathnames:However, it will not match
HELLO.C
orHellO.C
. To match is case insensitive pass the-iname
option as follows:Sample outputs:
Pass the
-type f
option to only search for files: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 usinggrep
command:OR try
Sample outputs: