[SOLVED] delete files older then 7 days (shell)

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

[SOLVED] delete files older then 7 days (shell)

Post by rsmarsha »

Just wondering if anyone can help me with a shell script.

I need to delete all csv files in a folder older than 7 days. I know how to delete and other basic shell stuff but not sure how to do it on files older than 7 days.

Any pointers would be great. :)
Last edited by rsmarsha on Fri Sep 12, 2008 9:42 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: delete files older then 7 days (shell)

Post by VladSun »

http://unixhelp.ed.ac.uk/CGI/man-cgi?find

You will need either -mtime, -ctime or -atime, depending on your needs.
Also, you may use

Code: Select all

find .... -exec command_here '{}' \;
Or

Code: Select all

find .... | xargs command_here
There are 10 types of people in this world, those who understand binary and those who don't
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Re: delete files older then 7 days (shell)

Post by rsmarsha »

Thanks,

Does this look right to you?

find $DIR -type f -atime +7 -exec rm -f {} \; -print
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: delete files older then 7 days (shell)

Post by VladSun »

Yes, it looks good :)
Just be carefull with it ;)
There are 10 types of people in this world, those who understand binary and those who don't
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Re: delete files older then 7 days (shell)

Post by rsmarsha »

Hehe had to make a bit of a change.

find /var/www/html/fredflintstone/exported_orders/done -mtime +6 -exec rm -f {} \;

was the one I settled on after a bit of googling. ;)

Thanks for the help
Post Reply