Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

A shell script to empty older items from the trash UNIX
After finally getting tired of remembering to "Empty Trash" at regular intervals, I decided to write a script (36KB download; source code on macosxhints) that automatically cleans files out of the trash after they've been there for seven days. It will even use sudo to clean out things that I don't have permission to erase -- if that option is available. There are many options to choose from, just use --help to see them all.

This script is designed to be very efficient to run, so I run it hourly. There is not (yet) any provision for maintaining the Trash at a particular size. I recommend adding a line to your cron file like this:
@hourly python /your/path/to/dirscan.py
This will clean things out after a week, and checks each hour for new items to remove. It pays attention only to top-level entries within .Trash, so any directories you put in the trash will be deleted automatically.

[robg adds: The code download link is from the author's site, and will be the best place to grab the latest version of the script. I mirrored the source as of today on macosxhints.com, just in case the parent site goes away at some point. Note that I haven't tested this one. Finally, for those interested in a size-limited trash can, this hint and the associated comments offer a few solutions.]
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[9,366 views]  

A shell script to empty older items from the trash | 18 comments | Create New Account
Click here to return to the 'A shell script to empty older items from the trash' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
A shell script to empty older items from the trash
Authored by: Hamid on Tue, Jun 19 2007 at 8:20AM PDT
TrashTimer allows you automatically empty Trash according to your settings from 1-7 hours/days/weeks/months:

http://www.hieper.nl/html/trashtimer.html

[ Reply to This | # ]
find ?
Authored by: Soliman on Tue, Jun 19 2007 at 10:16AM PDT

@hourly for cleaning the Trash seems a bit overkill, no ?

Here is what I have in my crontab:

### Everyday at 6.45
45 6 * * * /usr/bin/find /Users/Soliman/.Trash -mindepth 1 -maxdepth 1 -not -newerat '7 days ago' -delete

I use 'access time' as a reference (penultimate letter in newerat) and remove what is not newer (thus older) than 7 days.
You can add -ls if you want to know what was deleted...



[ Reply to This | # ]
find ?
Authored by: TonyT on Tue, Jun 19 2007 at 11:30AM PDT
Nice cron, but why "-mindepth 1 -maxdepth 1" ?

[ Reply to This | # ]
find ?
Authored by: Soliman on Tue, Jun 19 2007 at 11:43AM PDT

-mindepth 1 is to avoid removing .Trash itself :)

-maxdepth 1 could also be done with -prune (don't know if there is a difference here) and was added in order to remove directories that I've put to the Trash as one, and not file by file...



[ Reply to This | # ]
find ?
Authored by: TonyT on Tue, Jun 19 2007 at 1:41PM PDT
Thanks, I'll be adding this tonight!

[ Reply to This | # ]
find ?
Authored by: TonyT on Wed, Jun 20 2007 at 9:03PM PDT
Couldn't get it to work in crontab, but I was successful id creating a launchd LaunchAgent. Great tip, nice and simple. No need for a program or long script. Thanks!

If interested, the launchd script is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">;
<plist version="1.0">
<dict>
<key>Disabled</key>
<true/>
<key>Label</key>
<string>com.tony.DeleteTrash7DaysOld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/find</string>
<string>/Users/Tony/.Trash</string>
<string>-mindepth</string>
<string>1</string>
<string>-maxdepth</string>
<string>1</string>
<string>-newerat</string>
<string>-not</string>
<string>7 days ago</string>
<string>-delete</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceDescription</key>
<string>DeleteTrash7DaysOld</string>
</dict>
</plist>


[ Reply to This | # ]
find ?
Authored by: Soliman on Thu, Jun 21 2007 at 3:06AM PDT
Nice too, though I'm surprised it did not work in cron :(

---
--
Sylvain

[ Reply to This | # ]
find ?
Authored by: TonyT on Sat, Jun 23 2007 at 7:14AM PDT
Not sure why I couldn't get cron to work. It should have, but while trying to figure out the problem I leaned that Apple has depreciated cron since 10.4 in favor of launchd (a google or wiki will give background on this). Lingo, a nice freeware app, is helpful in writing launchd playlists. One advantage of using launchd over cron is that I can specify RunAtLoad (good for me as I have a laptop that I don't keep running 24/7)

[ Reply to This | # ]
find ?
Authored by: TonyT on Sat, Jun 23 2007 at 7:08AM PDT
I noticed an error.

Replace:
<string>-newerat</string>
<string>-not</string>
<string>7 days ago</string>

With:
<string>-not</string>
<string>-newerat</string>
<string>7 days ago</string>



[ Reply to This | # ]
find ?
Authored by: maxbook on Thu, Jun 21 2007 at 9:21PM PDT
wow. that's great.
i an really new to such stuff and i don't know what usr/bin/find is about? can you explain it to me?

[ Reply to This | # ]
find ?
Authored by: TonyT on Sat, Jun 23 2007 at 7:20AM PDT
find is a UNIX command locate in the usr/bin/ directory. When running cron or launchd you need the absolute path, hence usr/bin/find. To learn more about find, enter "man find" in Terminal

[ Reply to This | # ]
find ?
Authored by: TonyT on Mon, Jul 2 2007 at 8:51PM PDT
My trashed folders weren't deleting. It looks like find is changing the access time. changing -newerat to -newermt fixed that problem, but I guess if I trash a file without modifying it that it will get deleted sooner than anticipated.

[ Reply to This | # ]
find ?
Authored by: Soliman on Tue, Jul 3 2007 at 12:04PM PDT
This is strange, for me it works flawlessly and a ls -latu .Trash/ shows that the access time of my Trashed files did not change... could the problem be related to the launchd trick ? What happens if you just type the command in a Terminal ? (check access times before and after, do they change ?).

---
--
Sylvain

[ Reply to This | # ]

find ?
Authored by: TonyT on Tue, Jul 3 2007 at 5:31PM PDT
Same at the command line:

$ ls -latu
drwxr-xr-x 2 Tony Tony 68 Jul 3 20:26 untitled folder 2

/usr/bin/find /Users/Tony/.Trash -mindepth 1 -maxdepth 1 -not -newerat '7 days ago' -delete

$ ls -latu
drwxr-xr-x 2 Tony Tony 68 Jul 3 20:29 untitled folder 2

I'm running OS X 10.4.10 on an Intel MacBooh



[ Reply to This | # ]
find ?
Authored by: Soliman on Wed, Jul 4 2007 at 1:36AM PDT
Ok, I finally got it : for me it works because find doesn't change the atime of files but changes that of directories !!!

The solution ? Change -maxdepth 1 to -prune, which gives :

/usr/bin/find /Users/[blabla]/.Trash -mindepth 1 -prune -not -newerat '3 days ago' -delete :)

---
Sylvain

[ Reply to This | # ]

find ?
Authored by: TonyT on Wed, Jul 4 2007 at 12:47PM PDT
I had already tried -prune without success:

$ ls -latu
drwxr-xr-x 2 Tony Tony 68 Jul 4 15:39 untitled folder 2

$ /usr/bin/find /Users/Tony/.Trash -mindepth 1 -prune -not -newerat '7 days ago' -delete

$ ls -latu
drwxr-xr-x 2 Tony Tony 68 Jul 4 15:43 untitled folder 2

-newermt seems to be my only solution.

What ver of OS X are you running?


[ Reply to This | # ]
find ?
Authored by: Soliman on Thu, Jul 5 2007 at 1:20AM PDT
I run OSX 10.4.10, however if I didn't notice the problem it is probably because I didn't have any directory in my Trash, only "flat" files (and for them no access time change)...

To solve the directories problem something like the following might work :
/usr/bin/find -d .Trash -mindepth 1 \( -type d -and -empty \) -or \( -not -newerat '3 days ago' \) -delete

But it doesn't seem to work as I expected (-or problem ?), whereas separating the two commands does what it should...

---
Sylvain

[ Reply to This | # ]

find ?
Authored by: Soliman on Thu, Jul 5 2007 at 9:02AM PDT
Actually I discovered that the problem is caused by -delete so changing it to use rm seems ok.

Here is my new crontab entry :
/usr/bin/find /path/to/.Trash -mindepth 1 -prune -not -newerat '7 days ago' -exec /bin/rm -Rfv {} \;

DISCLAIMER: use rm -Rf at your own risk ;)

---
Sylvain

[ Reply to This | # ]