3

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

8

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*".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.