I'm trying to use the find command in Unix and I know how to use the basics of it. For example, I have four folders, A, B, C, and D. Under each folder I have a file called hi.dat. To look in all folders, I would do:

find * -name hi.dat.

Great! But now what if I do not want to look at folder D? Can I try something like:

find * not D/* -name hi.dat?

I do not want to type:

find A/* B/* C/* -name ... because I obviously have more than three folders and those were just used as an example.

Thanks! Amit

link|improve this question
feedback

2 Answers

up vote 9 down vote accepted

In Bash:

shopt -s extglob
find !(D) -name hi.dat

Ksh and zsh work similarly.

link|improve this answer
oh man. so do I type this in the command line? – Amit Mar 17 '11 at 21:42
3  
@Amit: Yes, where else? The extglob option may already be set. You can check by doing shopt extglob, if it says "on", you're set. If not, you can add the shopt -s extglob to your ~/.bashrc and it will be set for you when you start Bash. Once it's set by any of those methods, all you need to type is the find command. – Dennis Williamson Mar 17 '11 at 21:46
@Dennis: This is actually very convenient. So if I type what you wrote, this looks in all current files/directories under where I am currently located? – Amit Mar 17 '11 at 22:07
Oh this works like a charm. SO much easier to remember. – Amit Mar 17 '11 at 22:08
Could you elaborate a little more using this technique? What if I now didn't want A and D? How would that be written? – Amit Mar 17 '11 at 22:09
show 2 more comments
feedback

That would be -prune — but there's a slight trick to it:

$ find . \( -name D -prune \) -o -name hi.dat

-prune means "don't look any further on this path", so you need another branch for directories other than D. (-o means "or".) To skip other paths as well:

$ find . \( \( -name D -o -name Dminor \) -prune \) -o -name hi.dat
link|improve this answer
excellent, this worked!! Thank you. – Amit Mar 17 '11 at 21:44
+1 [to fill min] – Amit Mar 17 '11 at 22:08
feedback

Your Answer

 
or
required, but never shown

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