Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a find looking like this:

rm -f crush-all.js
find . -type f \( -name "*.js" ! -name "*-min*" ! -name "*console*" \) | while read line
do
   cat "$line" >> crush-all.js
   echo >> crush-all.js
done

I'd like to add to exclude a directory called "test" in the find but I can't seem to figure out how to add "-type d" somehow. How'd I go about doing that?

Thanks!

share|improve this question

4 Answers 4

up vote 6 down vote accepted

You can use the -path option to find and combine it with the -not operator.

find . ! -path "*/test/*" -type f -name "*.js" ! -name "*-min-*" ! -name "*console*"

Please note two things

  • -path must come as the first argument
  • the pattern matches the whole filename, so -path test will not match anything, ever

By the way, I'm not sure why you are using parentheses, it doesn't make a difference. It is only used for precedence, for constructs such as ! \( -name '*bla*' -name '*foo*' \) (that is, do not find things that have both bla and foo).

A further refinement: no need to use the bash loop, you can simply do

find . ... -exec cat {} \; -exec echo \;

where ... are the other arguments to find.

share|improve this answer

You can try with grep like this (the folder here is named test_folder):

find . -type f \( -name "*.js" ! -name "*-min*" ! -name "*console*" \) | grep -v "/path/to/test_folder/" | while read line

or if your find returns relative path :

find . -type f \( -name "*.js" ! -name "*-min*" ! -name "*console*" \) | grep -v "./relative_path/to/test_folder/" | while read line

or if you want all folders that has the same name but with different path

find . -type f \( -name "*.js" ! -name "*-min*" ! -name "*console*" \) | grep -v "/test_folder/" | while read line

Best regards,

share|improve this answer

To expand on what I think @CharlesClavadetscher was going for with his answer. If you omit a path with -not -path ... find will continue descending the path. Instead, using -prune will stop it from descending further into the omitted path, making it faster. So you can do something like

find . -path '*test/*' -prune -o -type f -name "*.js" ! -name "*-min*" ! -name "*console*" -print
share|improve this answer
find / -path ./test -prune -o ...

Rename ./test to fit the location of you test directory.

share|improve this answer

Your Answer

 
discard

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.