Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Ubuntu 10.04 lts

I am looking for the correct command line to delete all files from folder /var/www/myfolder/pictures/ every 30 days.

What is the command I should apply to crontab -e?

I am also using php5 on my server.

share|improve this question

closed as off topic by Chris S Jan 28 '12 at 3:49

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

I was told to use this command. Can someone validate? find /yourdir/tmp -mtime +1 -exec rm {} \; //where 1 is number of days since it was modified – Ubuntu User Apr 6 '11 at 3:07
Do you want to completely empty the folder every 30 days, or remove files that older than 30 days? – Phil Hollenback Apr 6 '11 at 3:44
Remove files older than 30 days. – Ubuntu User Aug 30 '11 at 11:35
up vote 4 down vote accepted

You can use find. This will find files older than 30 days and delete them

/usr/bin/find /var/www/myfolder/pictures/ -type f -mtime +30 -exec rm {} +

to run it on the 2nd of the month at 12:30 am from root's crontab (using crontab -l as root)

30 12 2 * * /usr/bin/find /var/www/myfolder/pictures/ -type f -mtime +30 -exec rm {} +

As you are using ubuntu you could also use /etc/cron.monthly/standard which is run at 52 6 1 * *.

# /etc/cron.monthly/standard: standard monthly maintenance script

# delete files older than 30 days from /var/www/myfolder/pictures/
/usr/bin/find /var/www/myfolder/pictures/ -type f -mtime +30 -exec rm {} +
share|improve this answer
Thank you to everyone. I would like to know if I can copy files from one location from another each day for pictures for that date only at a specific time: Lets say midnight I would like new files from /var/www/myfolder/original to /var/www/myfolder/actual – Ubuntu User Apr 7 '11 at 3:30

Find is your friend here.

find <path> -mtime +30 | xargs rm {}

You'll probably want to put some sanity checking around this in a script if you are going to put it in a script though.

share|improve this answer
what is xargs? I have not seen this before. Also this is going in crontab -e file. thank you – Ubuntu User Apr 6 '11 at 3:22
xargs lets you take a long list of arguments and run a command on them, above and beyond the normal argument limit number. In this particular case i dont think its needed though as find's -exec rm {}\ would also do the job, wouldn't it ? – Sirex Apr 6 '11 at 7:18
xargs would be useful if you had hundreds, or more of filenames being returned. It splits up the input and does it a few at a time. It's a great tool in a unix command-line arsenal. – Alister Bulman Apr 6 '11 at 8:03
+1 for applying with grace what I'm studying for lpic certification :D – Pitto Apr 6 '11 at 8:13

Aside from find, if you mean remove files older than a given date, you can also use tmpwatch, which is designed to remove files created or modified beyond a given time.

tmpwatch --mtime 720 /path/to/dir

you can also do 30d instead of 720 (hours)

Reading your actual question though it sounds like you want to remove all files every 30 days, rather than remove files older than 30 days, which isn't quite the same thing.

By 30 days, do you actually mean once a month ? If so, you can use @monthly in crontab and just run a rm on the folders contents.

@monthly <user> rm /path/to/dir/*

If not you can do:

0 12 */30 * * <user> rm /path/to/dir/*

Or you can fire tmpwatch off from cron (but it seems like you might as well use cron for the scheduling and just do an rm, it'll save cpu time just to prove everything in the dir is in fact new since the last time you removed everything, which seems a bit redundant to me). Should get you started

share|improve this answer

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