Repeating the Last Command as Root -- a Quick Intro to Event Designators with Video Example
Linux Training Academy
http://www.linuxtrainingacademy.com/
See the related blog post: http://www.linuxtrainingacademy.com/repeating-the-last-command-as-root/
If you ever forget to run a command with root privileges, you can simply repeat it by using sudo !! or su -c "!!".
$ adduser sam -bash: /usr/sbin/adduser: Permission denied $ sudo !! sudo adduser sam $ id sam uid=1007(sam) gid=1007(sam) groups=1007(sam) $ usedel -r sam -bash: /usr/sbin/userdel: Permission denied $ sudo !! sudo userdel -r sam $ id sam id: sam: No such user $ useradd jim -bash: /usr/sbin/useradd: Permission denied $ su -c "!!" su -c "useradd jim" Password: $ id jim uid=1007(jim) gid=1007(jim) groups=1007(jim)
This exclamation mark syntax is called an event designator. An event designator references a command in your shell history. Bang-Bang (!!) repeats the most recent command, but one of my favorite uses of the event designator is to run the most recent command that starts with a given string. Here's an example. Exclamation Mark
$ whoami jason $ uptime 12:33:15 up 35 min, 1 user, load average: 0.00, 0.00, 0.00 $ df -hT /boot Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 485M 55M 406M 12% /boot $ !u uptime 12:33:29 up 35 min, 1 user, load average: 0.00, 0.00, 0.00 $ sudo !w sudo whoami root
To learn more tips and tricks like this, check out my book Linux for Beginners or the video course by the same name.
https://www.youtube.com/watch?v=qCg1MLrAMJ0 ... https://www.youtube.com/watch?v=qCg1MLrAMJ0
5896372 Bytes