# Noob question, writing safe rm command.



## Alain De Vos (Mar 22, 2020)

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.

```
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 ...


----------



## eternal_noob (Mar 22, 2020)

Have you seen https://github.com/mattmc3/zsh-safe-rm ?


----------



## Alain De Vos (Mar 22, 2020)

Interesting, it's a bash script using mv , no cp,








						shell-safe-rm/rm.sh at 072f2844ca0a13f57fdd2ea1e9283f44f65c742b · kaelzhang/shell-safe-rm
					

😎 Safe-rm: A drop-in and much safer replacement of bash rm with nearly full functionalities and options of the rm command! Safe-rm will act exactly the same as the original rm command. - shell-safe...




					github.com


----------



## zirias@ (Mar 22, 2020)

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 `rm`s 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


----------



## Alain De Vos (Mar 22, 2020)

This worked,

```
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 $@
}
```


----------



## zirias@ (Mar 22, 2020)

Will fail on filenames containing spaces...


----------



## Alain De Vos (Mar 22, 2020)

I tried it on a file "a a" and a directory "b b" and remove * worked.


----------

