Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a terminal way. Open all files in /foo/bar containing the string foobar. How?

share|improve this question
up vote 9 down vote accepted

You didn't say how you want to open them :)

One way is to use grep to recursively look for the string in your files (-r). Use -l to ask grep to output only the filenames of matching files. This will output one filename per line, use xargs to build a suitable command line for your editor.

grep -r -l foobar /foo/bar/ | xargs vim

this also works with gedit; just change vim for gedit.

You can also use find for this but it's more complicated IMHO:

find /foo/bar -type -f -exec grep -l foobar {} \;  | xargs vim
share|improve this answer
Thanks, exactly what I was looking for. – mreq Feb 7 '13 at 23:46

Opens files containing foobar one by one in vim:

for f in $(find /foo/bar -type f); do 
    if [ ! -z "$(grep foobar $f)" ]; then
        vim $f 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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