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.
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.
Maybe you could use trash-cli.
You don’t have to have an alias for the original rm. \rm would do the same thing as the original rm.
Yup, indeed you are correct that there is a project hosted now that replicates this functionality (using the main trashcan to boot). Trash-cli was released after this quick bash script was written.
I still prefer the above mainly because: 1) you don’t have to install a package (making it portable within the .bashrc file) and 2) Personally I never use the FreeDesktop / Gnome trash-can anyway. Personal preferences rule and all.
You are correct that a backslash before any command instructs bash to use the regular command (not an alias), I neglected to mention this in the above article. I much prefer the alarming exclamation mark though to mentally add a ‘are you sure you want to do this’ stage. Again, merely a personal preference.
Hi, I didn’t try your script yet, but looks like you have a loop
If I’m right,
if you use rm in your function you’ll have an endless loop because the alias “rm=trash”
the line rm -r “$HOME/.trashcan/$1″ should be rm! “$HOME/.trashcan/$1″
because you second alias ‘rm!=/bin/rm -r’
Oddly Alias based commands do not use aliases themselves.
So the rm used in the ‘emptytrash’ is actually the real one.
Why don’t you simply use:
gvfs-trash another-useless-file
It will move file to gnome’s trash. The command is long? alias it to something short like “trash”
Wow, I didn’t know that… Thanks!
@anonymous: thanks you too for that tip.