Server Fault is a question and answer site for system and network administrators. 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 text I want to recursively search in mydir/. I would normally type grep -r "text to find" mydir/" but what would I type if I wanted to search all the files except a specific one?

For example, I do not want to search the file "myfile.txt" but it is contained in the directory.

What would I type?

share|improve this question
up vote 9 down vote accepted

You can achieve this by --exclude and --exclude-dir options of grep

For example:

grep -r --exclude-dir='<dirs_to_exclude>' PATTERN data


grep -r --exclude='<pattern_of_file>' PATTERN data 
share|improve this answer
You can also look for ack for code searching. – Abhijeet Kasurde Jul 13 '12 at 6:11

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.