12

I have a directory on my machine, think of it as my own sort of tmp directory, but it's in my ~ directory. And I want to make it so that my system every 3 hours deletes all the files in (though keeping any directory structure intact, but still deleting all the files in all the levels of the directories recursively) that directory which are older than a day.

I am running Ubuntu GNOME 15.10 with GNOME 3.18, can this be done? And if so, how? I would like this to be fully automated with no users interaction needed. This should be something automatically started when I login, so I shouldn't need to run something on every startup.

0

2 Answers 2

21

Using find:

find ~/tmp -type f -mtime +0 -delete
  • ~/tmp is the directory to be searched recursively, change this accordingly

  • -type f will look for only files

  • -mtime +0 which will match a file if it was last modified one day or more ago

  • -delete will just remove the matched file(s)

Here the catch is -mtime +0, most might think of using -mtime +1 but find will ignore any fractional time while calculating days. So, -mtime +1 will match a file if the last modification was made at least 2 days ago.

Quoting man find, -mtime has the same timing convention as -atime:

-atime n

File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

Also note that if you want precision, you should look at the -mmin option of find to indicate time in minutes.

To run it periodically after 3 hours, you can add a cron entry.

Run crontab -e and add:

00 */3 * * * /usr/bin/find ~/tmp -type f -mtime +0 -delete

Using zsh to remove the files:

rm ~/tmp/**/*(.-m+0)

Adding to cron:

00 */3 * * * /usr/bin/zsh -c 'rm ~/tmp/**/*(.-m+0)'
6

You should be able to delete all files older than 1 day in /home/username/directory and all directories below it with:

find /home/username/directory -type f -mtime +1 -delete

And to schedule that command every three hours set it as a cron job:

crontab -e

Then inside the crontab:

0 */3 * * * find /home/username/directory -type f -mtime +1 -delete

Which runs your command every three hours on the hour (i.e. minute 0), so 3:00 am, 6:00am etc.

Go the the cron and crontab manpages for more information on them.

4
  • 4
    This answer is actually wrong, please see the other answer.
    – user364819
    Dec 15, 2015 at 19:37
  • 4
    As this answer is wrong, and doesn't actually say anything more than the other answer, please either improve your answer so that it is correct and better than the other answer, or delete it.
    – user364819
    Dec 15, 2015 at 19:46
  • 5
    @ParanoidPanda I understand that you want high-quality answers, but commenting essentially the same thing twice is rather unneeded and could be seen as aggressive, especially for a new user. Dec 15, 2015 at 20:57
  • 2
    @RPiAwesomeness: Well, I commented the second time because I had run out of time to edit my first comment, and I also wanted to say that they should delete their answer which maybe wasn't something necessarily understood from my first comment, though I didn't want for them to feel under attack and like I was just saying that they had given the wrong answer and now must delete it, so I added the bit about improvement. But I can see how this could be taken the wrong way, so I ask you to remember: a Panda only gets aggressive when you take away his bamboo. ;-)
    – user364819
    Dec 15, 2015 at 21:05

You must log in to answer this question.