Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A shell script to empty the trash on a single volume UNIX
Here is a quick UNIX shell script which will empty the Trash on a single volume (disk) without disturbing the contents of the Trash on other volumes. I use it to empty the Trash on a Zip disk without disturbing my "main" (hard disk) Trash. As an example, if you name the shell script dumptrash, you could then empty the trash on a Zip disk called "Zip disk 1" by typing dumptrash "Zip disk 1". Remember to put quotes around the volume name if it has spaces, as in the example above. Your administrator password will be required for this operation.

#! /bin/sh
# Selectively dump the trash on named volume $1.

cd "/Volumes/$1"      # produce sensible error msg if input volume name not found
sudo rm -frv .Trashes/*
[robg adds: Remember to make the script executable with chmod +x script_name and store it somewhere on your path. I have not tested this script...]
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[6,583 views]  

A shell script to empty the trash on a single volume | 5 comments | Create New Account
Click here to return to the 'A shell script to empty the trash on a single volume' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A shell script to empty the trash on a single volume
Authored by: GaelicWizard on Feb 24, '03 03:11:50PM
This script is bad. It deletes all the trashes for all users on that disk, which, unless you are the ONLY user on the machine, is not good. This also uses 'sudo' which should only be used if you are trying to delete a difficult file. Also this cannot delete the trash in the root volume, as it is not mounted in /Volumes. A revision that sorrects all but that last point follows:

#!/bin/sh
# Selectively dump the trash on named volume $1.

cd "/Volumes/$1" # produce sensible error msg if input volume name not found
rm -frv "/Volumes/$1/.Trashes/$UID/*"

---
--
Pell

[ Reply to This | # ]

A shell script to empty the trash on a single volume
Authored by: Andrew Fletcher on Feb 25, '03 07:31:51AM

Anybody copying this script should check and double check that there is no space before the "*" on the rm line. If a space is inadvertently put there the script will remove all your files on that volume.

Andy



[ Reply to This | # ]
Danger will robinson
Authored by: SOX on Feb 25, '03 11:00:08AM

this script is still dangerous.
if the CD fails the RM takes place in the current directory.

you can make the result conditional like this

( cd somedirectoy ) && ( rm somefile)

also as another poster you do not check to the possibility of spaces in the UID environment variable. In a perfect world there would be none. but the consequences if there are any is that you erase the disk!



[ Reply to This | # ]
Danger will robinson
Authored by: GaelicWizard on Feb 26, '03 03:01:29AM

Your first concer is resolved by the absolute path in the 'rm' command, and your second is resolved by the "Quotes" around the path. ;)

JP

---

Pell



[ Reply to This | # ]
Danger will robinson
Authored by: oink on Nov 05, '03 11:19:46PM

From your suggestion, I make this:

( cd /Volumes/aaa/Trash ; pwd ) && ( rm -friv * ) ;( cd /Volumes/aaa/.Trashes ; pwd ) && ( rm -friv * ) ; ( cd /Trash ; pwd ) && ( rm -friv * ) ; ( cd /.Trashes ; pwd ) && ( rm -friv * ) ; ( cd ~/.Trash ; pwd ) && ( rm -friv * )

the pwd is just to make sure I am in the correct directory and I currently use the -i just to make sure everything is okay.

I am sure it is not the most efficient command, but I switch back and forth between 9 and x and have several drives attached and I seem to have trash cans under all sorts of names on each disk. I am hoping to be able to routinely run such a command (probably an alias) and do what I manually do currently...



[ Reply to This | # ]