Page 1 of 2

Delete files older than 7 days?

Posted: 2010/01/26 02:42:56
by gertrudis
Hi to all.
I've been trying to delete all files older than 7 days in a directory temp1.
I have try different ways but nothing happend:

find /temp1/* -mtime +7 -exec rm rf\
find . -mtime +7 -exec rm rf\
find /temp1/* -mtime +7 -print -exec rm{} \

can somebody pointme to the wright direction?

thanks in advance

Re: Delete files older than 7 days?

Posted: 2010/01/26 02:47:54
by cry4dawn
find /temp1/* -mtime 7 -print -exec rm -f {} \;

Re: Delete files older than 7 days?

Posted: 2010/01/26 03:30:59
by gertrudis
I try it and i looks like its working, but still files are in the folder

find /temp1/ -mtime 7 -print -exec rm -f {} \;

Re: Delete files older than 7 days?

Posted: 2010/01/26 05:04:39
by cry4dawn
use the asteric or:
find /temp1/ -mtime 7 -type f -print -exec rm -f {} \;

Re: Delete files older than 7 days?

Posted: 2010/01/26 15:10:18
by gertrudis
Doesn't work, I have try and try two with minutes and still the same problems, it does all proccess but no file are deleted.

I try it this:
find /temp1/ -mtime 7 -type f -print -exec rm -f {} \;

and with minutes too:
find /temp1/ -mmin 7 -type f -print -exec rm -f {} \;

Re: Delete files older than 7 days?

Posted: 2010/01/26 15:37:50
by toracat
The command provided by [b]cry4dawn[/b] should work fine.

Do you have the correct permission bits set for the files to be deleted and the directory they are in? Is there any chance that the filesystem is read-only? When you run the command, do you see any error message?

Re: Delete files older than 7 days?

Posted: 2010/01/26 18:51:47
by AlanBartlett
Two points. The [i]find[/i] manual page suggests that the braces may need to be escaped in certain circumstances and the [i]-print[/i] isn't necessary.

Hence I would use --

[code]
[b]find /temp1 -mtime 7 -type f -exec rm -f \{\} \;[/b]
[/code]

Re: Delete files older than 7 days?

Posted: 2010/01/26 22:14:36
by pschaff
If you want [b]older[/b] than 7 days, add "+"[code]
find /temp1 -mtime +7 -type f -exec rm -f {} \;[/code]

For 7 days or older
[code]
find /temp1 -mtime +6 -type f -exec rm -f {} \;[/code]

And I'd test first with "rm -i". :-)

Delete files older than 7 days?

Posted: 2010/01/26 22:18:50
by michaelnel
I'd test it first without any rm at all, just to see the list of files and inspect their age. On any "mass rm" kinda thing, I like to build up the line slowly to make sure it's finding what I want to operate on and nothing more.

Re: Delete files older than 7 days?

Posted: 2010/01/26 22:26:46
by pschaff
Good point:[code]
find /temp1 -mtime +7 -type f -ls
OR
find /temp1 -mtime +7 -type f -exec ls -l {} \;[/code]