0

I need to delete excel files which are older then 1 month. need to set cronjob. any suggestion for this? which script can be used to search excel files from particular directory and delete them.

1

Open up root's crontab with:

sudo crontab -u root -e

And create a new cronjob by appending this line to it:

60 * * * * find / -type f -iname "*.xls" -mtime +30 -exec rm -f {} \; &>/dev/null

With this you have created a cronjob that will be executed every hour and search your system with find for files with a .xls extension and is +30 days old, then it'll be removed with rm.

If you don't want the job to be executed every hour (60 min) you have to change this 60 * * * * to something else.

3
  • 1
    Best add -type f to the find before the -name to constrain the search to files which saves resources. Use -iname instead -name You forgot rm -f as for a lot of linux distributions rm is aliased to rm -i for root, and hence it would ask for each file. Either use -delete instead of -exec rm {}. Or, if your find version does not support -delete then better use -exec rm -f {} + as that makes quite a difference and does not spawn a subshell for each file it deletes. As the requester asked about Excel files in general, the pattern should be -iname "*.xls*".
    – doktor5000
    May 20 '15 at 7:38
  • Thanks for the tips @doktor5000. I've edited the find command so it has -type f, rm -f and -iname "*.xls.".
    – krt
    May 21 '15 at 9:26
  • Why not use -delete? You still spawn a subshell for every file found that is being deleted currently.
    – doktor5000
    May 21 '15 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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