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'm forwarding mail from a shared hosting account to a gmail address. The mail forwarder does not delete the mail after forwarding so the directory is getting full.

I tried the following in a cron job, but it only seems to delete one file at a time. I want to run the cronjob once a month and delete all files, not just the first file found.

find /home/myshare/mail/foo/new -type f -mtime 0 -print0 | xargs rm -f

I also tried this variation of the find command, but it also only deletes one file:

find /home/myshare/mail/foo/new -delete

My access to the server is via CPanel. How do I delete all files in a directory with a single command in a cronjob?

share|improve this question
up vote 0 down vote accepted

If you use the -print0 flag with find, you will want to use xargs -0.

share|improve this answer

how about find /home/myshare/mail/foo/new -type f -mtime 0 -exec rm -rf {} \;

or rm -rf /home/myshare/mail/foo/new/*

share|improve this answer
thanks for your suggestions. – LandedGently May 16 '11 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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