Cron job to delete all files/directories/subdirectories older than 1 day


jackfalveyiv

Recommended Posts

I would like to set up a cron job to delete all files and folders/directories older than 24 hours in my downloads directory.  I have made a few attempts to do this but cron is proving to be a bit tougher to understand than I previously expected.  I've tried a few versions of the following commands:


 

find /mnt/user/path/path/* -type d -mtime +0 -print0 -exec rm {} +

find /mnt/user/path/path/* -type f -mtime +7 -print0 | xargs -0 rm

find /mnt/user/path/path/* -mtime -1 -ls -exec rm -d {} +

find /mnt/user/path/path/* -mtime -1 -ls -exec rmdir -f {} +

 

I'm finding varying degrees of success, but essentially the downloads folder is broken into a few subdirectories.  I want to clean out those subdirectories of all files and folders older than 24 hours on a weekly scheduled job.  I have not been able to get this working the way I want.  I have run a combination of these scripts with some different options enabled, but I'm looking for one script to do what I need.  Does anyone have a better example of what I'm trying to accomplish?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.