Linux – find log files older than 30 days period

findlinux

I'm working on a script to find files older than 30 days in a given folder. Based on requirement i have to delete only files with extension .log or .out and skip sub directories.

The below command is returning all the .log* files that are modified 30 days ago. However it is returning all the .out files even the recent ones. is there something wrong in the below code?

    find -maxdepth 1 -mtime +30 -type f -name "*.log*" -o -name "*.out*"

could you let me know what is wrong in the above statement

Best Answer

Try:

find -maxdepth 1 -mtime +30 -type f \( -name "*.log*" -o -name "*.out*" \)

The problem was that find binds logical-and tighter than logical-or. The parens counteract that.

Without the parens, find is looking for files that match either (a) -mtime +30 -type f -name "*.log*" or (b) -name "*.out*".

With the parens, find is looking for files that (a) match -mtime +30 -type f and (b) match either -name "*.log*" or -name "*.out*".

Related Question