I understand that grep can only be used for searching for files which contain a string in their content.

Can it be used for searching for files with a string in their names? I read the manpage, and don't think so.

Also Is grep pronounced as [gri:p] or [grep] (in IPA notation)? I read it as the former.

  • 2
    The e sound is short: pronuncian.com/Lessons/default.aspx?Lesson=96 as in "bed", "said", "dead" -- not long (which would be greep). – goldilocks May 15 '14 at 13:16
  • grep is only able to search for patterns. You have to use other tools to generate the data which is then fed to grep to search for these patterns. – slm May 15 '14 at 14:05

You seem to be looking for find:

find . -type f -name "*foo*"

would look for file names containing foo in the name in the current directory and subdirectories.

find . -maxdepth 1 -type f -name "*foo*"

would look for file names containing foo only in the current directory.

| improve this answer | |
  • thanks. I know find can do it. I wonder if grep can do it? I don't think so, but someone said it could. – Tim May 15 '14 at 12:47
  • @Tim grep by itself can't. grep can if you pipe the list of files to it :) – devnull May 15 '14 at 12:49
  • Is it a good way? How do you say grep? – Tim May 15 '14 at 12:53
  • 2
    @Tim You have to understand that grep only looks at content, whether by opening files or in standard input. If you want grep to look at filenames, you have to put file names in its content. Either store the list of filenames in a file or pass it to grep on its standard input. Neither way is recommended - it is like using a hammer on a screw, i.e. wrong tool for wrong job. – jw013 May 15 '14 at 13:09
  • 1
    It makes no sense to have two different tools for the same thing. UNIX philosophy is »one job one tool«. If your job is finding a file, the tool is find. If it's finding something inside a file, it's grep. – Andreas Wiese May 15 '14 at 14:38

Use find to generate a list of filenames and then grep to parse them:

find . | grep 'stack[oO0]verfl[oO0]w'

Unless you have a filesystem where the list of filenames is itself a file I don't see how grep can do that alone.

| improve this answer | |
  • 3
    But you should add that find . -name 'stack[oO0]verfl[oO0]w' would be a bit more efficient, as it does not need two processes running. (There is a small difference, as the grep variant will match the complete path name, not only the filename.) – Dubu May 15 '14 at 21:05

Just for fun, you can use grep itself to generate the names of (non-empty) files in the current directory and spawn another grep to look for the ones you want:

grep -l . * 2>/dev/null | grep 'your_pattern'

The 2>/dev/null is needed because grep will complain if you ask it to look in a directory without supplying the -r option.

Caveat emptor: this will perform poorly with files that have newlines in their name and will ignore dot (hidden) files and empty files. find is the right way to go, here.

| improve this answer | |

I don't know if that would be useful, and I can't comment so...

ls | grep [regexPattern]

exempli gratia: search for files that initiate with the letter 'p':

ls | grep ^p

And can be mixed in a lot of different way(with regex) to search what you want to find, I remember that I read an article that says to not parse the output of the ls, so be careful.

| improve this answer | |
  • 1
    For finding files by different criteria (amongst them its name) there's been something invented called, well… find. – Andreas Wiese May 15 '14 at 14:36
  • Yea, it's a better way than feeding the grep with a ls(parsing ls at the same time), thank you! :) – Rafael Campos Nunes May 15 '14 at 14:39
  • @AndreasWiese ls | grep is a bit simpler to type than find -mindepth 1 -maxdepth 1 -name – user60101 May 15 '14 at 20:41
  • Yes indeed, but find is better, i just came up with this option of parsing the ls with the grep :p – Rafael Campos Nunes May 15 '14 at 21:52
  • @RafaelNunes Better in what way? ls | grep is perfectly fine – user60101 May 15 '14 at 22:37

To answer your question, no. Grep cannot search via the names of files, it can only search through textual data as it's presented to it. So you basically have 2 options with grep.

You can either have it search through a list of files that you present to it, and grep will open each of these files in turn, looking for the pattern that you've instructed it to look for, or you can feed it a stream of data via STDIN.

In either case grep cannot look at the "meta data" around the source of its input (STDIN or files), only their contents.

| improve this answer | |

GNU grep has an option to only --include specific filenames, but you still have to provide grep with a pattern to search for. For example

grep -r --include '*.txt' PATTERN .

will recursively traverse the current working directory and print any lines matching PATTERN within files ending in .txt

| improve this answer | |

You can use find with grep :

for example :

┌─ oneofone@Oa [~/c/g/s/g/O/cleanml] (master|●4✚3)                                                                
└──➜ find -type f -name '*.go' -exec grep -n package '{}' +
./html/doc.go:1:package html
./html/element.go:1:package html
./scanner/tree.go:1:package scanner
./scanner/tokenizer.go:1:package scanner
./main.go:1:package main

Or just grep : grep package **/*pat* *pat*

Or you could use ack if you're searching for code : ack package --type=go

| improve this answer | |
  • **/*pat* *pat*. Isn't the second *pat* redundant with globstar on? – user60101 May 26 '14 at 18:22
  • I'm not very sure, I don't use that much. – OneOfOne May 26 '14 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.