Is there a way to block rm
with a single *
in production servers? This would help prevent accidents like:
rm test *
instead of
rm test*
Is there a way to block rm
with a single *
in production servers? This would help prevent accidents like:
rm test *
instead of
rm test*
Use zsh --- it automatically asks you if you want to delete all the files in a directory.
server:~/dir/processing> rm *
zsh: sure you want to delete all the files in /home/wheel/dja/dir/processing [yn]?
(I assume this is a default feature --- I can't find any config options that have been set to turn it on, but I didn't write my .zshrc)
Here's a bash function that can be sourced from .bashrc to add a warning when you use rm with more than 2 arguments:
unalias rm 2>/dev/null
real_rm=/bin/rm
rm_opts=""
function confirm {
echo -n "Do you want to continue (Y/N)? "
read v
v=$(echo $v|tr '[a-z]' '[A-Z]')
if [[ "$v" == "Y" ]]; then
return 0
elif [[ "$v" == "N" ]]; then
return 1
else
confirm
fi
}
function rm {
if [ $# -gt 2 ]; then
echo "WARNING: You have passed a list of files and directories that is $# entries long! Is this what you intended?"
echo "Here is the list of files:"
echo "$@"
confirm
if [ $? -eq 0 ]; then
$real_rm $rm_opts $@
fi
else
$real_rm $rm_opts $@
fi
}
Not without replacing the shell. rm
doesn't even see the *
since the shell globs the appropriate filenames before passing them to rm
.
for a in $@; do echo $a; done
you would see many many lines if * is passed.
Jun 13, 2011 at 13:20
Not a solution, but small workaround.
Alias rm
as rm -i
.
This workaround does not help when flag -f is used.
You could also provide a special alias that user's who you are concerned will make this mistake can use instead. e.g.
alias rm-test="rm test*"
Again it's more of a work around, but, in general *nix assumes you know what you're doing and mean to do what you say.
saferm
alias we set up?" -- of course it's too late then :).
Jun 13, 2011 at 14:34