16

In order to reduce disk space usage, I want to automate a temporary clean in my Downloads folder. I figured two ways to do so:

1) Changing the configurations of firefox, etc. to save files to /tmp/ (this would require, for safety, changing the variable TMPTIME in /etc/default/rcS to 7 or more days);

2) Turning the ~/Downloads folder into a temporary directory that behaves similarly to /tmp/, deleting old files. The problem is that in /tmp files are indiscriminately deleted in the end of the session; in ~/Downloads folder it would be better to delete files by their creation date.

I'm not very sympathetic to the first option, since it requires a lot of config. I'd like some help to implement the second one. What's the best way to do it?

1 Answer 1

21

Instead of changing how the directory works, you could have a little clean-up script. It's easier to implement and probably less dangerous in the long run.

The following will delete anything over 7 days old in your ~/Download/ directory:

find ~/Download/ -mtime +7 -delete

You might want to test that by just removing the -delete segment and checking the files it returns. But once you're happy with it, you can schedule it to run once a day by running crontab -e and adding this on a new line:

@daily find ~/Download/ -mtime +7 -delete

ControlX then Y to save and exit and you're done.

3
  • 6
    The tmpwatch or tmpreaper package are better approaches to cleaning up a directory, having been designed for exactly this purpose.
    – MikeyB
    Dec 14, 2011 at 18:41
  • +1 @MikeyB, but I would also put this in a script which runs at every boot, since a cronjob won't run if the machine is off when the job is due.
    – scottl
    Jan 4, 2012 at 7:41
  • 1
    @scott why not just duplicate the line with @reboot instead of @daily to have the command run at boot as well May 19, 2019 at 8:43

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.