How to run find -exec on files that have been modified for more than 15 days?

Asked

Viewed 174 times

2

I am trying to remove some files from a directory, but I cannot mount a command. What I want would be more or less like this:

find  -name '*.log*' -mtime 15 -exec rm -f {} 

Find the default name and remove the modified files more than 15 days ago. I tried the above command, but it returns that there are missing parameters in -exec.

1 answer

2


First of all, -mtime 15 brings the files modified exactly 15 days ago. If you want the ones that were modified there is plus 15 days, use -mtime +15.

As for the option -exec, lacked to put a \;:

find  -name '*.log*' -mtime 15 -exec rm -f {} \;

Or a +:

find  -name '*.log*' -mtime 15 -exec rm -f {} +

The difference is that the first option executes the command rm -f once for each file found, while the second option passes several file names at once to the rm -f, reducing the number of times the control rotates.


Another option is to make one pipe (|) and use the command xargs:

find  -name '*.log*' -mtime 15 |xargs rm -f

With that, the exit of find (filenames) are passed as parameters to the command rm -f (similar to the option with + above).


View documentation for more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.