Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1451  / 1 Year ago, tue, may 9, 2023, 1:05:50

I have noticed in all my Ubuntu servers the alias rm -i is ignored when you run sudo rm * as root. Is there something in sudo that is causing this behavior? I know you don't need to use sudo when you are root however in the event that a Jr SA were to do so it would remove the contents of the directory. Also knowing that rm in Ubuntu does not preserve / it could mean a total system meltdown.



Example as root:



johndoe@hostname:/tmp/foobar $ sudo su -
root@www3d:~# cd /tmp/foobar
root@hostname:/tmp/foobar# for i in 'a b c d e f g' ; do touch $i ; done
root@hostname:/tmp/foobar# sudo rm *
root@hostname:/tmp/foobar# for i in 'a b c d e f g' ; do touch $i ; done
root@hostname:/tmp/foobar# rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
rm: remove regular empty file `d'? y
rm: remove regular empty file `e'? y
rm: remove regular empty file `f'? y
rm: remove regular empty file `g'? y
root@hostname:/tmp/foobar# for i in 'a b c' ; do touch $i ; done
root@hostname:/tmp/foobar# rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
root@hostname:/tmp/foobar# for i in 'a b c' ; do touch $i ; done
root@hostname:/tmp/foobar# sudo rm *
root@hostname:/tmp/foobar# ls
root@hostname:/tmp/foobar# exit
logout


Example as user:



johndoe@hostname:/tmp/foobar $ for i in 'a b c' ; do touch $i ; done
johndoe@hostname:/tmp/foobar $ rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
johndoe@hostname:/tmp/foobar $ for i in 'a b c' ; do touch $i ; done
johndoe@hostname:/tmp/foobar $ sudo rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y

More From » sudo

 Answers
3

There is a very neat trick to solve this problem. Use the alias for sudo as follow.



alias sudo="sudo "
#Trailing space at the end.


Reason from the credit page at the end of post:




A trailing space in value causes the next word to be checked for alias
substitution when the alias is expanded




.



Example



user@user-desktop:~/test$ for i in 'a b c d e f g' ; do touch $i ; done
(reverse-i-search)`al': un^Cias -a
user@user-desktop:~/test$ alias rm="rm -i"
user@user-desktop:~/test$ rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
r m: remove regular empty file `c'? y
rm: remove regular empty file `d'? y
rm: remove regular empty file `e'? y
rm: remove regular empty file `f'? y
rm: remove regular empty file `g'? y
user@user-desktop:~/test$ for i in 'a b c d e f g' ; do touch $i ; done
user@user-desktop:~/test$ alias sudo='sudo '
user@user-desktop:~/test$ sudo rm *
rm: remove regular empty file `a'? y
rm: remove regular empty file `b'? y
rm: remove regular empty file `c'? y
rm: remove regular empty file `d'? y
rm: remove regular empty file `e'? y
rm: remove regular empty file `f'? y
rm: remove regular empty file `g'? y


Credits: arch wiki


[#41692] Tuesday, May 9, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
peafowkes

Total Points: 356
Total Questions: 102
Total Answers: 117

Location: Lebanon
Member since Tue, Oct 12, 2021
3 Years ago
peafowkes questions
;