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
add comment

3 Answers

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
add comment

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
add comment
find / -path ./test -prune -o 

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

Enjoy.

share|improve this answer
add comment

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.