Exclude directories in FIND command

Can you please help tweak the below command to exclude all directories with the name "logs" and "tmp"

find . -type f \( ! -name "*.tar*" ! -name "*.bkp*" \) -exec /usr/xpg4/bin/grep -i "user_1" /dev/null {} + >result.out
bash-3.2$ uname -a
SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v

I would prefer a single line command executed on the terminal and not a script with multiple line. But either is fine.

Did you consider the negated -path test, or the -prune action?

I did try -path but it fails

find . -type f \( ! -name "*.tar*" ! -name "*.bkp*" ! -path "*/logs/*" \) -exec /usr/xpg4/bin/grep -i "user_1" /dev/null {} + >result.out
find: bad option -path
find: [-H | -L] path-list predicate-list

I don't know how to use -prune here. Can you help ?

This is immediately from man find :

Do you feel able to adapt it to your problem?

Well, in this case it is

find . -type d \( -name "logs" -o -name "tmp" \) -prune -o ... 

Did you discard the exclusion of files that i mentioned in the OP ?

You seem to have only mentioned exclusion of directory while i wanted exclusion of files (which works in the OP command) along with the directories.

Kindly help.

---------- Post updated at 03:36 PM ---------- Previous update was at 03:34 PM ----------

I am not able to understand your suggestion so as to make changes to include the directories along with the files. Can you tweak the command in the OP to include directories is what i m seeking help for.

Please help.

---------- Post updated at 03:36 PM ---------- Previous update was at 03:36 PM ----------

I am not able to understand your suggestion so as to make changes to include the directories along with the files. Can you tweak the command in the OP to include directories is what i m seeking help for.

Please help.

Hi,

Can you please try this ?

file list:

$ mkdir logs logs1 tmp tmp1
$ ls
basic.log  file1.bkp  file2.bkp  logs  logs1  tar1.tar  tar2.tar  tmp  tmp2  try.sh
find . ! -path "logs*" ! -path "tmp*" -type f \( ! -name "*.bkp" ! -name "*.tar" \)

Gives output:

How about this:

find . \
   -type d \( -name "logs*"  -o -name "tmp*"   \) -prune -o \
   -type f \( -name "*.tar*" -o -name "*.bkp*" \) -prune -o \
   -type f -exec /usr/xpg4/bin/grep -i "user_1" /dev/null {} + >result.out

eg:

$ find . -type f -print
./basic.log
./file1.bkp
./logs/file1
./logs.test
./logs1/file2
./src/code.c
./tar2.tar
./tmp/test.txt
./tmp1/testing
./try.sh

$ find . \
   -type d \( -name "logs*"  -o -name "tmp*"   \) -prune -o \
   -type f \( -name "*.tar*" -o -name "*.bkp*" \) -prune -o \
   -type f -print
./basic.log
./logs.test
./src/code.c
./try.sh

The -prune primary in find is ignored when the current file is not a directory. So you can delete the text shown in red above and get the same results.

1 Like

I am very sorry that we did NOT serve the turnkey solution on a silver plate but assumed you could apply some of your own creativity to follow the path indicated to come to a solution of your own. Esp. when MadeInGermany indicated where to continue, which is: add the rest of your find command from before.