Delete files older than 7 days?

General support questions including new installations
gertrudis
Posts: 6
Joined: 2010/01/26 02:13:07
Contact:

Delete files older than 7 days?

Postby gertrudis » 2010/01/26 02:42:56

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

cry4dawn
Posts: 108
Joined: 2009/09/23 01:48:56

Re: Delete files older than 7 days?

Postby cry4dawn » 2010/01/26 02:47:54

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

gertrudis
Posts: 6
Joined: 2010/01/26 02:13:07
Contact:

Re: Delete files older than 7 days?

Postby gertrudis » 2010/01/26 03:30:59

I try it and i looks like its working, but still files are in the folder

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

cry4dawn
Posts: 108
Joined: 2009/09/23 01:48:56

Re: Delete files older than 7 days?

Postby cry4dawn » 2010/01/26 05:04:39

use the asteric or:
find /temp1/ -mtime 7 -type f -print -exec rm -f {} \;

gertrudis
Posts: 6
Joined: 2010/01/26 02:13:07
Contact:

Re: Delete files older than 7 days?

Postby gertrudis » 2010/01/26 15:10:18

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 {} \;

User avatar
toracat
Forum Moderator
Posts: 7154
Joined: 2006/09/03 16:37:24
Location: California, US
Contact:

Re: Delete files older than 7 days?

Postby toracat » 2010/01/26 15:37:50

The command provided by cry4dawn 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?

User avatar
AlanBartlett
Forum Moderator
Posts: 9299
Joined: 2007/10/22 11:30:09
Location: ~/Earth/UK/England/Suffolk
Contact:

Re: Delete files older than 7 days?

Postby AlanBartlett » 2010/01/26 18:51:47

Two points. The find manual page suggests that the braces may need to be escaped in certain circumstances and the -print isn't necessary.

Hence I would use --

Code: Select all

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

pschaff
Retired Moderator
Posts: 18276
Joined: 2006/12/13 20:15:34
Location: Tidewater, Virginia, North America
Contact:

Re: Delete files older than 7 days?

Postby pschaff » 2010/01/26 22:14:36

If you want older than 7 days, add "+"

Code: Select all

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


For 7 days or older

Code: Select all

find /temp1 -mtime +6 -type f -exec rm -f {} \;


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

michaelnel
Posts: 1478
Joined: 2006/05/29 16:50:11
Location: San Francisco, CA

Delete files older than 7 days?

Postby michaelnel » 2010/01/26 22:18:50

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.

pschaff
Retired Moderator
Posts: 18276
Joined: 2006/12/13 20:15:34
Location: Tidewater, Virginia, North America
Contact:

Re: Delete files older than 7 days?

Postby pschaff » 2010/01/26 22:26:46

Good point:

Code: Select all

find /temp1 -mtime +7 -type f -ls
OR
find /temp1 -mtime +7 -type f -exec ls -l {} \;


Return to “CentOS 5 - General Support”

Who is online

Users browsing this forum: No registered users and 3 guests