261

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/'

jww
  • 97,681
  • 90
  • 411
  • 885
Dru
  • 9,632
  • 13
  • 49
  • 68
  • This question has more answers here: [How can I recursively find all files in current and subfolders based on wildcard matching?](https://stackoverflow.com/q/5905054/11725753) – EvgenKo423 Jul 06 '21 at 18:06

8 Answers8

442

Use find:

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.

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

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:

ls | grep touch

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

Zagorax
  • 11,440
  • 8
  • 44
  • 56
  • 7
    @Dru, if you want it 'shorter' you can avoid `-print` as this is the default behaviour and `.` as this is the default folder where it checks. – Zagorax Jul 04 '12 at 12:41
  • Awesome. I see myself using this a lot. I will take your `-print` and `.` removal suggestions, make it a command, and try to pass `*string*` in as a command line argument. – Dru Jul 04 '12 at 12:43
  • 2
    `find . -name "*string*"` Works great too. Removing `.` throws an error on my end. Thanks again @Zagorax. – Dru Jul 04 '12 at 13:49
  • Just an observation, the above command complained about the position of `-maxdepth` argument better to move it before `-name` as @Sunil Dias mentioned – Saikat Jan 06 '16 at 12:54
  • I have `find *.jpg -name "*from*" -print` which works for a given directory. How can I make search recursively? I've tried `-maxdepth`. – zadrozny Jan 22 '16 at 16:53
  • bro maxdepth does down only 1 step lol that's silly. Might as well use -r or do like maxdepth 9000 to really go all the way down, no? Other than that, thanks for the great response – Nick Res Mar 22 '17 at 18:39
  • It would be better if you could mention what each of the option for find mean. – Riyafa Abdul Hameed May 17 '17 at 09:14
  • You might want to ignore case with `ls | grep -i touch` – RaSor Oct 07 '18 at 10:59
23

Use grep as follows:

grep -R "touch" .

-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.

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • Great. I noticed that some file contents follow a `:`. Is there anyway to withhold that? Using an option perhaps? – Dru Jul 04 '12 at 12:22
  • 1
    Try: `grep -R "touch" . | cut -d ":" -f 2` – carlspring Jul 04 '12 at 12:23
  • 1
    That seems to only produce the contents of the files. You essentially answered my question though, I can try to do some digging for withholding the contents. – Dru Jul 04 '12 at 12:27
  • Ah... you only need the file names? Run :`grep -R "touch" . | cut -d ":" -f 1` (sorry must have misread you). – carlspring Jul 04 '12 at 12:31
  • Thanks @carlspring this is interesting. `grep` either returns files with contents and filenames containing `touch` or contents containing `touch`, I'm not sure which is the case, yet. Of the list of files returned, half contain `touch` in the title and the other half conatains `touch` in the body, not the title. Just realized this. – Dru Jul 04 '12 at 12:37
  • `grep` scans files for a certain pattern. The output is: `foo.txt: some text matching your pattern` -- it basically outputs the line where it found the match. – carlspring Jul 04 '12 at 12:38
  • They asked for file name containing the string not the actual content of file. – Pranjal Sahu Jun 25 '21 at 19:50
  • @sahu, well you can always further process this by appending `| cut -d ":" -f 1`, so I don't see the need for the downvote. – carlspring Jun 27 '21 at 09:30
  • Won't work for binary files like in my case. Also please edit your answer that will help future readers instead of digging in the comments section. – Pranjal Sahu Jun 28 '21 at 15:22
7

The -maxdepth option should be before the -name option, like below.,

find . -maxdepth 1 -name "string" -print
Anton
  • 6,349
  • 1
  • 25
  • 53
Sunil Dias
  • 523
  • 8
  • 9
4
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:

/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:

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:

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:

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
shilovk
  • 11,718
  • 17
  • 75
  • 74
0

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

$ compgen -f .bash
.bashrc
.bash_profile
.bash_prompt
Zombo
  • 1
  • 62
  • 391
  • 407
  • 4
    `compgen` is not an appropriate hammer for this nail. This little-used tool is designed to list *available commands*, and as such, it lists files in the *current* directory (which could be scripts) and it can neither recurse nor look past the beginning of a file name nor search file contents, making it mostly useless. – msanford Mar 24 '14 at 22:05
0

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.

**/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.

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.

kvantour
  • 25,269
  • 4
  • 47
  • 72
-1
find / -exec grep -lR "{test-string}" {} \;
Zoe
  • 27,060
  • 21
  • 118
  • 148
Shahid
  • 1
-3
grep -R "somestring" | cut -d ":" -f 1
Saurabh kukade
  • 1,608
  • 12
  • 23
  • 3
    This appears to be an answer to a different question - but because you've provided no explanation, that isn't obvious to the asker or to anyone else. – Toby Speight Sep 18 '17 at 10:00