Quantcast
Viewing all articles
Browse latest Browse all 17

Trash with Bash

One of the regrettably unavoidable aspects of the Unix shell like environments is the impedance between what a user says and what a user means. While this is present in all computing environments the sheer power of Unix based command lines make a potential mistake catastrophic.

Who amongst us has not at some point done something like:

> rm * .tmp

or

> rm *>tmp

The former makes the mistake of a space between the dot and the ‘tmp’ part and the latter the accidental holding down of the shift key while pressing the intended full stop.

While searching for an alternative for the I-have-done-this-too-many-times-and-now-its-embarrassing approach of nuking everything with the ‘rm’ command its time i did something about it.

Inserting the following in your ~/.bashrc file will remap the ‘rm’ command to a slightly safer move-to-trash like behavior. Its not comprehensive but it has saved my life on more than one occasion.

?Download .bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
# Trash support by Matt Carter <m@ttcarter.com
# Source and information: http://hash-bang.net/2009/03/trash-with-bash
function trash() {
	if [ -d "$HOME/.trashcan/$1" ]; then
		# Already exists in bin - remove
		rm -r "$HOME/.trashcan/$1"
	fi
	mv --target-directory=$HOME/.trashcan/ -- "$@"
}
alias "rm=trash"
alias "emptytrash=/bin/rm -rf $HOME/.trashcan/* $HOME/.trashcan/.??*"
alias 'rm!=/bin/rm -r'
mkdir ~/.trashcan 2>/dev/null

Now the command ‘rm’ moves files into ~/.trashcan. You can also use the command ‘rm!’ when you really mean delete immediately and the utility command ‘emptytrash’ to clean everything out.


Viewing all articles
Browse latest Browse all 17

Trending Articles