Emptying the Trash - part II
Here's a more advanced shell script for emptying the trash in Mac OS X. It deletes the stubborn files & folders that refuse to be erased because they are either 1) locked or 2) have incorrect permissions or 3) both. It doesn't have any problems with spaces in the path name, so it should be safe. It can only be run by users who have administrative access.
It works great on Macs that have multiple partitions or hard drives.
There are three easy steps to set this up in the Terminal:
1) Make an executable directory:
mkdir ~/bin
chmod 700 ~/bin
2) copy the script below, and paste it into a text editor (like BBEdit Lite), and then save it with the name "trash" in your new "bin" directory (make sure to save with unix line breaks). If you want you can use the "pico" editor in the Terminal:
pico ~/bin/trash
now copy from the web page, and paste it into the Terminal. Exit pico and save the changes with the commands "Ctrl-x", then "y".
3) Make the script executable:
chmod +x ~/bin/trash
rehash
Now you can empty the trash any time by opening a Terminal window and giving the following command:
trash
Here's the script
code:
------------------------------------------------------------------------
#!/bin/sh
##
# Mac OS X script to empty the trash of stubborn items
# Please send suggestions for improvements to
# testuser at
http://www.macosx.com
##
# Make checks before running the script
UID=`id -u`
if [ "$UID" -eq 0 ]
then
printf "Error: this script cannot be run as root or with sudo\n"
printf "Usage: trash\n"
exit 1
fi
# Check for trash on main and other volumes
printf "Must authenticate to empty trash. "
sudo printf ""
if [ -n "`ls ~/.Trash 2> /dev/null`" ]; then
mainTrash="full"
fi
if [ -n "`sudo find /Volumes/*/.Trashes/${UID}/* 2> /dev/null`" ]; then
otherTrash="full"
fi
if [ "$mainTrash" != "full" -a "$otherTrash" != "full" ]; then
printf "No items were found in the trash.\n"
exit 2
fi
# Empty the trash on the main volume; the one that is running OS X
if [ "$mainTrash" = "full" ]; then
printf "Emptying trash on main volume.\n"
sudo chflags -R nouchg ~/.Trash/* 2> /dev/null
sudo rm -Rf ~/.Trash/*
else
printf "No trash found on main volume.\n"
fi
# Empty the trash on other volumes; mounted at /Volumes
if [ "$otherTrash" = "full" ]; then
printf "Emptying trash on other volumes.\n"
find /Volumes/*/.Trashes/${UID}/* -prune -exec sudo chflags -R nouchg {} \;
find /Volumes/*/.Trashes/${UID}/* -prune -exec sudo rm -Rf {} \;
else
printf "No trash found on other volumes.\n"
fi
------------------------------------------------------------------------
Last edited by testuser on 02-11-2002 at 07:27 AM