How to delete folders that are older than a day? (Cron Job)

12,347

Solution 1

This should do it:

find /path/to/dir -maxdepth 0 -ctime +1 -exec rm -fr {} +

But be careful, and test it first outside of cron, without the -exec part, so you don't delete something else by accident.

Solution 2

First see which files will be deleted:

find /path/to/dir -type d -mtime +1 -print

Then once ready to execute:

find /path/to/dir -type d -mtime +1 -print0 | xargs -0 rm -r
Share:
12,347

Related videos on Youtube

Joe Scotto
Author by

Joe Scotto

Writer of code, creator of videos, and taker of photos.

Updated on August 11, 2022

Comments

  • Joe Scotto
    Joe Scotto over 1 year

    I want to delete a folder and its contents on my host if it is over a day old. The issue is that I have no experience on how to do this with a linux cron job. I should also say that i have looked at google and nothing comes up. I think this is a pretty simple question so please, help me out. Thanks

    • Joe Scotto
      Joe Scotto about 10 years
      This is for files, not folders.
    • paxdiablo
      paxdiablo about 10 years
      No, you would have to use rm -rf.
  • Joe Scotto
    Joe Scotto about 10 years
    What would I modify to change the amount of days? The +1 part? Also this is running on a webserver.
  • Joe Scotto
    Joe Scotto about 10 years
    I am using cpanel, for the directory would it be somthing like public_html/website/uploads ?
  • janos
    janos about 10 years
    @KronoFiles that really depends on your hosting provider. If a command has output in cron, then cron usually emails the output to use. You can run find public_html/website without any other arguments to print all the files and subdirectories in there and it should get emailed to you. Based on that output you can figure out the right path to use.
  • Joe Scotto
    Joe Scotto about 10 years
    Would I fill my directory in in both /path/to/dir?
  • Joe Scotto
    Joe Scotto about 10 years
    Is there any website where I could learn more about this? I'm really interested now.
  • Joe Scotto
    Joe Scotto about 10 years
    Also, is there a way that I can delete it if it is over a minute or seconds?
  • janos
    janos about 10 years
    @KronoFiles if you have a Linux/UNIX/Mac then you can read about this in man find. Or on this site: linuxmanpages.com/man1/find.1.php
  • janos
    janos about 10 years
    @KronoFiles it seems you can use -cmin and -mmin to specify in minutes instead of days. But not in seconds.
  • Joe Scotto
    Joe Scotto about 10 years
    Does it matter which one? and how would I structure it?
  • janos
    janos about 10 years
    @KronoFiles this page explains the difference between ctime and mtime: linux-faqs.info/general/…
  • Joe Scotto
    Joe Scotto about 10 years
    I'm running this command to test it but I am getting this: find: /public_html/kronofiles.com/uploads/1/': No such file or directory and find: missing argument to -exec'`
  • Joe Scotto
    Joe Scotto about 10 years
    It emailed me this find: /public_html/kronofiles.com/uploads/1/': No such file or directory rmdir: missing operand Try rmdir --help' for more information.
  • janos
    janos about 10 years
    @KronoFiles I knew you would figure that one out ;-)
  • Manuel Kießling
    Manuel Kießling about 10 years
    Can you run find /public_html/kronofiles.com/uploads/1/ and paste the output unto pastebin.com, and tell me the Pastebin link?