Skip to content
Advertisement

Combining find command conditions

I am trying to combine 2 find commands to pipe to a grep pattern match.

my 3 commands are:

get files modified in the last 24 hours:

find '/logs' -mtime 0 -type f

ignore a couple of directories:

find -type d ( -path /dir -o -path /dir2 -o -path /dir3 ) -prune -o -print

find pattern in the file’s lines:

grep -rnw -e "my pattern"

I have gotten below to work but cant seem to add directory skipping into the mix.

get files modified in the last 24 hours and find pattern in the file’s lines:

find '/logs' -mtime 0 -type f | xargs grep -rnwI -e "my pattern"

So ideally I would like to combine getting files modified in the last 24 hours and ignoring directories.

Advertisement

Answer

find '/logs' ! -path "*/dir1/*" ! -path "*/dir2/*" ! -path "*/dir3/* -mtime 0 -type f | xargs grep -rnwI -e "my pattern"

or

find '/logs' -mtime 0 -type f | egrep -v "dir1|dir2|dir3" | xargs grep -rnwI -e "my pattern"
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement