Aliasing rm to rm -i for sudo

Markus Kolb ubuntu-ml at tower-net.de
Mon Jun 20 18:23:18 UTC 2005


Michael Beattie wrote on Mon, Jun 20, 2005 at 11:51:58 -0400:
> On 6/20/05, Markus Kolb <ubuntu-ml at tower-net.de> wrote:
> > Matthew S-H wrote on Mon, Jun 20, 2005 at 10:24:47 -0400:
> > > try adding:
> > > alias "sudo rm"="sudo rm -i"
> > 
> > That's an invalid alias name. "sudo rm" can't be a command.
> > 
> 
> you could do
> 
> alias sudo_rm='sudo rm -i'
> 
> but that's a bit weird.

Another possibility would be to write a shell script which checks
arguments. If argument $1 is "rm" you can add a "-i" and call
/usr/bin/sudo with this new string.

Then alias sudo='/usr/local/bin/mysudoscript'.

This script should work with many shells supporting the used variable
substitution. At least bash and ksh.

#!/bin/sh
#
# mysudoscript
#
SUDO="/usr/bin/sudo"

if [[ ${1} == "rm" ]]; then
    CM="${1} -i ${@#${1}}"
elif [[ ${1} == "mv" ]]; then
    CM="${1} -i ${@#${1}}"
else
    CM="${@}"
fi

$SUDO $CM






More information about the ubuntu-users mailing list