1

What I am trying to do is do a search on my server to find which files contain mentions of images with a certain keyword in the filename but all I can figure out is how to find the filenams (not image specific), and not the files that contain references to them.

What I have tried is:

find -name *nasa*

but that's so far off what I'm looking for, any ideas?

2
  • Are you trying to search files for a regex? A recursive grep -R is all you need. Or do you want help building the regex itself? We have no idea what these files you speak of look like, or the directory structure, so any additional information would be helpful.
    – jw013
    Commented Nov 13, 2012 at 17:25
  • @jw013 - I wanted to do a search recursively to check for any files using an image with a filename like nasa.extension and list the files that contained such a reference Commented Nov 13, 2012 at 20:07

2 Answers 2

4

With a modern implementation of grep:

grep -r nasa .

Here is a nasty way to check if it looks like an image filename:

grep -rE 'nasa[[:alnum:] ]*\.(jpe?g|png|gif|tif)' .

There are some obvious caveats (the biggest one being that it only allows alphanumeric characters and spaces in the filename to avoid false positives with syntax).

Although that said, if this is HTML or something, you'd be better to use a parser.

5
  • is there anyway to limit the results to the filenames with references to images with 'nasa' in their filename? A bit tricky I think but just wondering if it's possible Commented Nov 13, 2012 at 16:32
  • 1
    @martincarlin87 if you want accurate regex examples, always provide an example of what you are trying to match. We have no idea what "references" means in the context of your files.
    – jordanm
    Commented Nov 13, 2012 at 16:36
  • @martincarlin87 Updated my answer, would be nice if you specified what a "reference" was.
    – Chris Down
    Commented Nov 13, 2012 at 16:38
  • sorry, I'll try to be specific. So, starting from the current directory and searching recursively, output the names of any files found to contain *nasa*.png|jpg|gif - does it make more sense now? Commented Nov 13, 2012 at 16:38
  • @martincarlin87 Okay, my updated answer should work then.
    – Chris Down
    Commented Nov 13, 2012 at 16:39
0

Try this with appropriate replacements per your requirements:

find /path/to/ -type f -exec grep -i 'string' {} +

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .