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 have: a directory with many nested subdirectories and files.

I would like: a list of all files whose name doesn't contain a certain string.

Example file structure:


If I enter my query with the string "test" it should return only /foo/bar/foobar.txt

I suppose one would use find with some regex?

Thanks a lot for your help!

share|improve this question
up vote 2 down vote accepted

Depends on what you want in the output.

ls -R | grep -v test


find foo -type f | grep -v test

You can format the output with tools such as perl or awk

share|improve this answer
Thanks, that perfectly does the job! – vgXhc Feb 1 '13 at 16:54

If you want to get

a list of all files whose name doesn't contain a certain string

you have to go with something like this:

find foo/ -type f -exec bash -c "basename {} | grep -v 'test' > /dev/null && echo {}" \; This will match only by filename, but output a relative path as find would normally do.
May be someone can propose a more elegant solution?

If matching against relative path is okay you can go with the answer, provided by @bodhi.zazen:

find foo -type f | grep -v test

share|improve this answer
Thanks for this option, too, but for my purposes @bodhi.zazen has provided a sufficient solution. – vgXhc Feb 1 '13 at 16:58

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.