Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacLadybug

macrumors 6502a
Original poster
Jun 6, 2008
633
28
I would like to be able to (with one click) automate the process of dumping my trash from the terminal... Right now I do it using these commands:
Type "cd ~/.Trash" (then I hit enter)
Type "rm -r *" (hit enter again)

I've never written a script or a workflow... I can't seem to figure out how which app would be better for this or how to write it. If someone would show me how, then I'm sure I can figure out how to write other ones on my own.

Is there something I could read that would explain everything step-by-step?

Any help would be appreciated.

Thanks.
 
There is no difference between the GUI method of emptying the trash and the command line.

Your way is kinda unnecessary already.

I haven't used AppleScript in a while buy it'd be something like:

Do shellscript "rm -r* ~/.Trash"
 
In AppleScript you can empty the trash this way:

Code:
tell application "Finder"
	empty
end tell
 
Sometimes dumping the trash can take forever...

There is no difference between the GUI method of emptying the trash and the command line.

Your way is kinda unnecessary already.

I haven't used AppleScript in a while buy it'd be something like:

Do shellscript "rm -r* ~/.Trash"

I don't have to do the cd ~/.Trash part first?

I use that method of trash emptying when I have a ton in the trash and I don't want to wait around for it to empty. I can have thousands of items in it and it takes a second.
 
I don't have to do the cd ~/.Trash part first?

I use that method of trash emptying when I have a ton in the trash and I don't want to wait around for it to empty. I can have thousands of items in it and it takes a second.
I repeat: There is no difference, other than the placebo effect, of emptying the trash from the command line vs a GUI method.


There is NO difference.

There is no need for "cd ~/.Trash" because you can select the directory while running the command. Kill two birds with one stone, really.
 
I repeat: There is no difference, other than the placebo effect, of emptying the trash from the command line vs a GUI method. There is NO difference. There is no need for "cd ~/.Trash" because you can select the directory while running the command. Kill two birds with one stone, really.

Thanks. I'm always afraid I might be missing something when it comes to these things. So many times, certain things are just understood. But in this areana I am a real novice and I want to be sure I understand. Not to mention it would suck if erased my whole hard drive... if that were possible.

Anyway, thanks for taking the time to help.
 
There is no need for "cd ~/.Trash" because you can select the directory while running the command. Kill two birds with one stone, really.

Except that the command given:
Code:
Do shellscript "rm -r* ~/.Trash"
is wrong.

It's exceedingly dangerous to make guesses about command-line recursive deletion. You can kill a lot more than two birds.

I recommend this single line that combines the original two lines:
Code:
cd ~/.Trash; rm -r *
It can be stored in a simple text file, then copied and pasted into Terminal.

There are several other ways of doing this, too, but given the command-line experience level, copy-paste seems the safest.
 
Except that the command given:
Code:
Do shellscript "rm -r* ~/.Trash"
is wrong.
I did state that my AppleScript was a little rusty. I'm not near a computer I can test this at anyway.


It's exceedingly dangerous to make guesses about command-line recursive deletion. You can kill a lot more than two birds.
if someone who is obviously not familiar with the command line, let alone bash, is willing to do such simple tasks with it, then I'm not willing to give the obvious disclaimers.

I recommend this single line that combines the original two lines:
Code:
cd ~/.Trash; rm -r *
It can be stored in a simple text file, then copied and pasted into Terminal.

There are several other ways of doing this, too, but given the command-line experience level, copy-paste seems the safest.

An AppleScript would be about as safe, if it's copied and pasted. Again I'm not near a computer, but one could test my code and see if it works correctly.
Code:
do shell script "cd ~/.Trash; rm -r *"
 
Makes a perfect droplet!

Thanks all... I used: do shell script "cd ~/.Trash; rm -r *" and made the script I wanted... saved it as an .app and now I have the perfect droplet on my finder window to dump big volumes of trash in seconds. Works perfectly!

It also gave me a better general understanding of how to write these commands to use scripts... which I hadn't done before and have wanted to learn more about them and how to create them. It's a start anyway. Thanks for your patience.
 
Just remember that your script does nothing that the normal method of emptying the trash does.

Yep, but when I have a butt load of stuff and it takes 15 minutes to empty... the script does it instantly in one click.
 
Yep, but when I have a butt load of stuff and it takes 15 minutes to empty... the script does it instantly in one click.

It actually doesn't. Just because there's no GUI telling you it's taking 15 minutes doesn't mean it isn't. The command line is just the GUI free method, it takes just as long.
 
It's exceedingly dangerous to make guesses about command-line recursive deletion. You can kill a lot more than two birds.

Quoted for truth and for the humorous turn of phrase. :)

cd ~/.Trash; rm -r *

This command would be significantly improved by substituting the ; for a &&.

When separating two commands with a semi-colon, the shell merely runs the two commands in series. Using an && to separate the commands will cause the shell to only run the second command if the first command is successful.

Imagine a scenario where there is no ~/.Trash directory, due to corruption or the $HOME being mangled or some other reason. With the ; separator, the cd command would fail but the system would happily still run the following rm command in the user's home directory or current working directory.

I suggest:

Code:
 cd ~/.Trash && rm -r *

Although I don't see anything wrong with:

Code:
rm -r ~/.Trash/*

as a single-command alternative.
 
^^^
I completely agree. Well described.


One addendum:
The trash on other disks isn't emptied when ~/.Trash/* is removed. Examples: internal partition, external HD, USB thumb drive, network share, Firewire drive. I mention this only for completeness.
 
Yep, but when I have a butt load of stuff and it takes 15 minutes to empty... the script does it instantly in one click.

Just one question .... If it takes 15 minutes to empty via the GUI, are you sure you don't have "secure delete" option set, as that will then take a lot longer in the GUI, whereas the "rm" command is a straight delete so is always quicker.
 
Yes I use Secure Empty...

So, now I've changed the Script to:
do shell script "srm -vrmf ~/.Trash/*"

Seems to work fine.
 
Again. The script already ships with OS X and it works more efficiently, because it doesn't have to run in AppleScript. If you would just use the Finder you could save yourself some minor resources.
 
Again. The script already ships with OS X and it works more efficiently, because it doesn't have to run in AppleScript. If you would just use the Finder you could save yourself some minor resources.

After all the fiddleing around, you are 100% correct. I can't shortcut how long it takes for a bunch of junk to "secure" empty. If there is a bunch of stuff (movies, etc.) it's going to take just as long when it's done "securely". So it's just click the secure empty button in trash or set finder preferences to "secure empty" trash and wait it out. I guess there is no way to just flush it fast cause it overwrites the space.

Thanks for the patience. I did learn a lot a few new things along the way. I had never tried to work with Applescripts before, so this was new and interesting anyway.
 
If you're interested in AppleScript this is a good place to start. If you're interested in shell scripts here is a good place to start.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Thanks for the references. I'll definitely order one of those to get started.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.