LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automatically deleting trash (http://www.linuxquestions.org/questions/linux-newbie-8/automatically-deleting-trash-553358/)

akamad 05-12-2007 08:02 AM

Automatically deleting trash
 
Hi all. Is it possible to write up a bash script to automatically delete any items from the trash that are older than, say a month? Thanks.

fukawi2 05-12-2007 08:52 AM

Code:

find ~/.Trash -mtime +30 -exec rm -Rf {} \;
-mtime +30 means find anything last modified more than 30 x 24 hours ago (ie, 30 days), then -exec passes that file (via the {} identifier) to 'rm -Rf'

No Guarantees! Try replacing '-exec rm -Rf {} \;' with just '-print' to make sure what it finds is actually what you want to delete first!

akamad 05-12-2007 11:27 PM

Thanks for that! It works brilliantly!

taxtropel 05-12-2007 11:41 PM

The above find command is all fine and dandy but you don't wanna type this in all the time...set up this command as a cron job. cron is the daemon which handles tasks on a regular basis, such as backups and time updates and deleting
.Trash folders

for this command you wanna do something like this:

Code:

crontab -e
which will bring up the crontab editor (for the current user, so if you want this system wide then be root when you do this)

once inside the editor you want the command format of
minute hour dayOfmonth month dayOfweek command
use a '*' for times you don't care about (such as what day of week)
example
Code:

42 4 1 * * find ~/.Trash -mtime +30 -exec rm -Rf {} \;
will run the command at 4:42am on the first of each month

once your command is setup in crontab then save and exit
and you are set

akamad 05-16-2007 01:53 AM

Thanks for that! First time I've come across cron, it seems very handy! Aah...there's so many commands in Linux systems.

gothicbob 05-16-2007 05:19 AM

if the computer isnt on at the particular time the cron job is supposed to happen, will it be executed at next logon? or ignored untill the next time?

chrism01 05-16-2007 07:06 AM

Without RTFM the man page I believe it'll wait until the next 1st of mth. Remember cron was really designed when Unix only existed on servers ie pretty much 24 x 7 boxes.
Otoh, you can '*' the day of mth field and/or pick a more likely time of the day...
Ah, the flexibilty of Linux :)

fukawi2 05-16-2007 07:45 AM

If your system has anacron installed, it will run any scheduled tasks that were missed while the PC was off... Can be good, can be annoying :P

Cron itself won't "backdate" scheduled tasks.


All times are GMT -5. The time now is 11:08 AM.