LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 05-12-2007, 09:02 AM   #1
akamad
LQ Newbie
 
Registered: Mar 2007
Distribution: Debian Squeeze
Posts: 16

Rep: Reputation: 0
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.
 
Old 05-12-2007, 09:52 AM   #2
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Rep: Reputation: 34
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!
 
Old 05-13-2007, 12:27 AM   #3
akamad
LQ Newbie
 
Registered: Mar 2007
Distribution: Debian Squeeze
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks for that! It works brilliantly!
 
Old 05-13-2007, 12:41 AM   #4
taxtropel
Member
 
Registered: Mar 2005
Location: Cascade Mountains WA USA
Distribution: Linux From Scratch (LFS)
Posts: 149

Rep: Reputation: 16
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

Last edited by taxtropel; 05-13-2007 at 01:04 AM.
 
Old 05-16-2007, 02:53 AM   #5
akamad
LQ Newbie
 
Registered: Mar 2007
Distribution: Debian Squeeze
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks for that! First time I've come across cron, it seems very handy! Aah...there's so many commands in Linux systems.
 
Old 05-16-2007, 06:19 AM   #6
gothicbob
Member
 
Registered: Sep 2005
Location: Birmingham / Bath Uk
Distribution: Kubuntu 8.10, Debain 4.0
Posts: 86

Rep: Reputation: 16
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?
 
Old 05-16-2007, 08:06 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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
 
Old 05-16-2007, 08:45 AM   #8
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Rep: Reputation: 34
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.

Last edited by fukawi2; 05-16-2007 at 11:19 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove trash icon from desktop or change trash iscon cad Linux - General 7 02-27-2007 12:50 AM
Deleting files from command line goes to trash? HSN Linux - Enterprise 1 02-19-2007 12:38 AM
Deleting from trash can KezzerDrix Mandriva 1 03-27-2005 11:12 PM
I changed the trash can icons but now the full trash can icon doesn't show? Why? Ausar Linux - Newbie 6 08-04-2004 02:59 PM
Everything went to the trash!!! Gerardo Mandriva 1 01-22-2004 05:52 PM


All times are GMT -5. The time now is 01:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration