Linux find command : how to use multiple conditions

Hello.
I need to find all files but excluding some because

  • I need to exclude some sub-folders
  • I need to exclude some filenames
  • Files must be within two dates.
  • The result is sent to a function

I cannot achieved to put together the date conditions, the folder conditions and the files conditions.

By focusing on the clause of the directories I get the expected results.

rm -rfv /tmp/000_outfile.txt
sudo find /home/user_bidon  -type d \( -iname .cache  -o -iname .mozilla  -o -iname .nv  -o -iname .pki  -o -iname gwenview  -o -iname pulse  \
  -o -iname "*session*"  -o -iname thumbnail.so  -o -iname RecentDocuments  -o -iname Trash  -o -iname sddm  -o -iname kscreen  \ 
  -o -iname  view_properties  -o -iname Music  -o -iname Pictures  -o -iname Public  -o -iname Templates  -o -iname Videos  -o -iname bin  \
  -o -iname Desktop  -o -iname Documents  -o -iname Downloads  \)  -prune -o -print | sort  2>&1 | tee /tmp/000_outfile.txt
cat /tmp/000_outfile.txt | wc -l

But when I try to add first the date conditions, that failed ( no output at all ) :

rm -rfv /tmp/000_outfile.txt
sudo find /home/user_bidon    -newer /tmp/foo_deb  ! -newer /tmp/foo_end  \(   -type d \( -iname .cache  -o -iname .mozilla  -o -iname .nv  \
-o -iname  .pki  -o -iname gwenview  -o -iname pulse -o -iname  "*session*"  -o -iname thumbnail.so  -o -iname RecentDocuments  \
-o  -iname Trash  -o -iname sddm  -o -iname kscreen -o  -iname  view_properties  -o -iname Music  -o -iname Pictures  -o -iname  Public  \
-o -iname Templates  -o -iname Videos  -o -iname bin -o -iname Desktop  -o -iname Documents  -o -iname Downloads  \)  -prune -o -print \)  \
| sort  2>&1 | tee /tmp/000_outfile.txt
cat /tmp/000_outfile.txt | wc -l

In short :

find somepath  [  DATE CONDITIONS  ]  \( -type d [ \( FOLDER CONDITIONS -o FOLDER CONDITIONS .... \) ] -prune -o -print \) 

Secondly where to add the file conditions after the folder conditions

....... \( -type f  \( ! -name ".Xauthor*" ! -name ".xsession*" \) \) ..........

Any help is welcome

Try:

find path \( \
   \( [condition A] \) -prune -o \
   \( [condition B] \) -prune -o \
   \( [condition C] \) -prune \
\) -o -print

Example:

$ mkdir -p date_chk/{one,two}/{in,out,Documents,Music}
$ touch -t 201901011033 date_chk/{one,two}/{in,out}/oldy
$ touch date_chk/{one,two}/{in,out,Documents,Music}/{okfile,.xsession_notOK,.Xauthor}
$ touch -t 201903011000 foo_deb
$ touch -t 201912011000 foo_end
$ find ./date_chk \( \
     \( -newer foo_end -o ! -newer foo_deb \) -prune -o \
     \( -type d \( -iname Documents -o -iname Music \) \) -prune -o \
     \( -type f \( -name ".xsession*" -o -name ".Xauthor*" \) \) -prune \) -o -print
./date_chk
./date_chk/one
./date_chk/one/in
./date_chk/one/in/okfile
./date_chk/one/out
./date_chk/one/out/okfile
./date_chk/two
./date_chk/two/in
./date_chk/two/in/okfile
./date_chk/two/out
./date_chk/two/out/okfile
4 Likes

Thank you very much.
Nice demo.

PS a dash is missing at the last line :

\) -o print

must be

\) -o -print
2 Likes

Thanks,

I've updated my post to include the missing hyphen, so any others trying it don't waste time trying to work out the issue.

It is also worth pointing out that any of the conditions A thru C that match a directory will ignore anything under the directory as well.

So for example if a directory had a mod time outside of the foo_deb to foo_end range all contents below that point would be excluded from the find.

For example:

$ mkdir -p date_chk/{one,two}/{in,out,Documents,Music}
$ touch -t 201901011033 date_chk/{one,two}/{in,out}/oldy date_chk/one
$ touch date_chk/{one,two}/{in,out,Documents,Music}/{okfile,.xsession_notOK,.Xauthor}
$ touch -t 201903011000 foo_deb
$ touch -t 201912011000 foo_end
$ ./date_chk
./date_chk/two
./date_chk/two/in
./date_chk/two/in/okfile
./date_chk/two/out
./date_chk/two/out/okfile

Everything under date_chk/one is now discarded due to the folder being out of the date range! If you have file level criteria these need to follow the folder stuff and proceed the print like this:

find path \( \
   \( [folder-condition A] \) -prune -o \
   \( [folder-condition B] \) -prune -o \
   \( [folder-condition C] \) -prune \
\) -o \( \
   \( [file-condition D] \) -o
   \( [file-condition E] \) \
\) -o -print

Another example

$ find ./date_chk \( \
     \( -type d \( -iname Documents -o -iname Music \) \) -prune -o \
     \( -type f \( -name ".xsession*" -o -name ".Xauthor*" \) \) -prune \) \
     -o \(  \
        -newer foo_end -o ! -newer foo_deb -o ! -type f \) -o -print
./date_chk/one/in/okfile
./date_chk/one/out/okfile
./date_chk/two/in/okfile
./date_chk/two/out/okfile

Now we get only File types within range, regardless of the time on the parent folder(s).