Noob question, writing safe rm command.

I want to write an alias for the dangerous /bin/rm , which forces copy to ~/.Trash , followed by force remove
It should by preference work on files,directories,wildcards.
I tried in .zshrc this but it does not work correctly.
Code:
alias rm='echo "rm is disabled use remove instead"'
alias remove='cp -vf -- $1 ~/.Trash; /bin/rm -ivf -- $1'
I thought maybe I could use rsync ?
Feel free ...
 
Well, first of all, you could skip the -f flag. In general, there's a reason if a file doesn't have write permission for your user, and automatically changing that if possible might not be what the user intended.

Then, rm -ivf makes no sense at all, the -f flag overrides the -i flag. Also, by using $1, you cripple rms ability to remove multiple files at once (which will also not work any more with shell-expanded wildcards).

And finally, what you do is ultimately moving, you could just use mv for that.

Or, you could do what most people do and just set an alias to rm -i, which makes sure that for interactive use, you will be asked before actually removing a file. IMHO, this is much less annoying than the concept of a "trashcan". Of course, setting this alias named rm has the drawback that for interactive use, you HAVE to give the -f flag if you want to skip the safety questions. I personally don't use such aliases any more and just pay attention :D
 
This worked,
Code:
alias rm='echo "rm is disabled use remove instead"'
remove(){
mydate=`date | tr ' ' '.' `
mydir="/usr/home/x/Trash/${mydate}"
mkdir -v $mydir
echo "Copy:"
cp -axfvR $@ $mydir
echo "Remove:"
/bin/rm -vR $@
}
 
Back
Top