How-To Exclude Directory in find command

How can i tweak the below find command to exclude directory/s -> "/tmp/logs"

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) -print

Note: -path option/argument does not work with the version of find that i have.

bash-3.2$ uname -a
SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v 

find . \( ! -type d \)

Can you tweak my original command to add the condition of excluding this directory -> "/tmp/log" ??

Does your version support the -prune action?

Yes.

it supports -prune

But, i m not sure how can i use -prune with the given command.

Your request is not clear.

Are you trying to exclude the contents of the specific directory /tmp/logs (which would only be a problem with the script you showed us if you were sitting in / when you invoked your script), or are you trying to exclude the contents of any directory logs that is located in a directory named tmp .

Either way a grep may be the easiest way to solve your problem:

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) | grep -v '^./tmp/logs/'

or:

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) | grep -v '/tmp/logs/'

depending on your answer to the above question.

Note, however that the first one above may exclude other directories from your search if you are not located in / when you invoke find .

1 Like

@Don: Thank you for the suggestion.

But where can i put the -print option which you can see in the OP ?

Becoz in the next line i was reading the output file as you can see below.

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) -print |
while read file
do

Please suggest.

I suggest that you read post #6 and decide whether you want to use:

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) | grep -v '^./tmp/logs/' |
while IFS= read -r file
do	: Do whatever you want to do with "$file"
done

or:

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) | grep -v '/tmp/logs/' |
while IFS= read -r file
do	: Do whatever you want to do with "$file"
done

based on your answer to the question I asked in post #6. If you really, really want to, you can change those suggestions to:

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) -print | grep -v '^./tmp/logs/' |
while IFS= read -r file
do	: Do whatever you want to do with "$file"
done

or:

find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) -print | grep -v '/tmp/logs/' |
while IFS= read -r file
do	: Do whatever you want to do with "$file"
done

respectively, and get exactly the same results with more typing. (In cases where you do not specify any primaries to find that produce output (such as -exec , -ok , or -print ), find supplies a -print primary by default.)

I sincerely apologize for assuming that you would realize that I was just suggesting a couple of possible replacements for your find command (based on your answer to the question about what files you are trying to exclude) in your pipeline. Based on the title of this thread, I didn't realize that I needed to repeat and improve the rest of your pipeline as well.