Linux find command : how to use multiple conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux find command : how to use multiple conditions
# 1  
Old 05-29-2019
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.
Code:
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 ) :


Code:
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 :
Code:
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


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



Any help is welcome
# 2  
Old 05-30-2019
Try:
Code:
find path \( \
   \( [condition A] \) -prune -o \
   \( [condition B] \) -prune -o \
   \( [condition C] \) -prune \
\) -o -print


Example:

Code:
$ 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


Last edited by Chubler_XL; 05-30-2019 at 07:22 PM.. Reason: Missing hyphen on last print parameter - thanks jcdole
These 4 Users Gave Thanks to Chubler_XL For This Post:
# 3  
Old 05-30-2019
Thank you very much.
Nice demo.


PS a dash is missing at the last line :
Code:
\) -o print

must be
Code:
\) -o -print


Last edited by jcdole; 05-30-2019 at 06:30 AM..
These 2 Users Gave Thanks to jcdole For This Post:
# 4  
Old 05-30-2019
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.
# 5  
Old 05-30-2019
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:

Code:
$ 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:

Code:
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
Code:
$ 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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple conditions in IF

Fellas, Am new to unix os/ and here the situation , I am trying to write multiple condition statement inside if but it throws me a error here is my piece of code , if ] && ] && ] then commands fi error : line 15 : ` can someone please advise me how to fix it Please use... (7 Replies)
Discussion started by: xeccc5z
7 Replies

2. UNIX for Dummies Questions & Answers

If + multiple conditions

Hello Unix-Forums! It has been a long time since my last post, but finally I've got a new question: I know in case you can use multiple patterns by case $var in a|b|c|ab) and so on. But how would I place an OR between if ] then ... if ] then ... I want to execute the "..." if... (3 Replies)
Discussion started by: intelinside
3 Replies

3. UNIX for Dummies Questions & Answers

Multiple Find Conditions in IF Statement - UNIX

When I try the below if Condition with single condition its working fine. But when I try to Club both its working . But giving wrong results. In my case cond1 = -f ${filename1} = true cond2 = -f ${filename2} = true But Cond1 & Cond2 is resulting in False ??? Please advise ... (5 Replies)
Discussion started by: Shiny_Reddy
5 Replies

4. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

5. UNIX for Advanced & Expert Users

linux find and replace in multiple files

Is there a reason why all of these examples of linux find and replace in multiple files use find and grep to pipe into sed or perl. Why not just use sed or perl directly with something like this? sed -i 's/echo/burnbaby/g' booboo*... (2 Replies)
Discussion started by: cokedude
2 Replies

6. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

7. Linux

Simplified find command to find multiple file types

Hi, I'm using the following command to find the multiple requierd file types and its working fine find . -name "*.pl" -o -name "*.pm" -o -name "*.sql" -o -name "*.so" -o -name "*.sh" -o -name "*.java" -o -name "*.class" -o -name "*.jar" -o -name "*.gz" -o -name "*.Z" -type f Though... (2 Replies)
Discussion started by: vickramshetty
2 Replies

8. Shell Programming and Scripting

multiple if conditions

Guys, Im trying to have a script that evaluates multiple conditions : test.sh: if then echo "host $1" else if then echo "host $1" else echo $1 not valid exit 1 fi when I do ./test.sh brazil1 I get: (4 Replies)
Discussion started by: bashshadow1979
4 Replies

9. UNIX for Dummies Questions & Answers

Multiple conditions in find or ls stmts

I'm looking for syntax to include two search patterns in a find or ls command. e.g. find BTIME_ACTUAL_HRS* OR BTIME_SCHEDULED_HRS* tia (5 Replies)
Discussion started by: mavsman
5 Replies

10. UNIX for Dummies Questions & Answers

multiple conditions in if/then

Hello, I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated! if ; then array=("${array}" "$dnNum" ) fi i receive this error: ./testscript: ' (4 Replies)
Discussion started by: grandtheftander
4 Replies
Login or Register to Ask a Question