Running previous command with sudo

Very often in linux, we execute a command only to see an error displaying ‘Permission denied.’ Then we realize that we had forgotten sudo before the command and we write the command again, this time with a sudo infront.

Examples:

rohan@Rohan-PC:~$ apt-get update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - 
SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open 
(13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - 
RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - 
RemoveCaches (13: Permission denied)

rohan@Rohan-PC:~$ sudo apt-get update
[sudo] password for rohan: 
Get:1 http://packages.microsoft.com/repos/vscode stable InRelease [2, 802 B]
Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease                
Hit:3 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease 
Ign:4 http://dl.google.com/linux/chrome/deb stable InRelease           
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Hit:6 https://deb.nodesource.com/node_8.x xenial InRelease             
Hit:7 http://ppa.launchpad.net/hanipouspilot/rtlwifi/ubuntu xenial InRelease
Hit:8 http://dl.google.com/linux/chrome/deb stable Release             
Get:9 http://packages.microsoft.com/repos/vscode stable/main amd64 Packages [77.9 kB]
Hit:10 http://ppa.launchpad.net/maarten-baert/simplescreenrecorder/ubuntu xenial InRelease
Get:12 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Hit:13 https://download.sublimetext.com apt/stable/ InRelease          
Hit:14 http://ppa.launchpad.net/noobslab/apps/ubuntu xenial InRelease  
Get:15 http://archive.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Hit:16 http://ppa.launchpad.net/noobslab/macbuntu/ubuntu xenial InRelease
Hit:17 http://packages.treasuredata.com/3/ubuntu/xenial xenial InRelease
Get:18 http://nginx.org/packages/ubuntu xenial InRelease [4, 339 B]     
Ign:18 http://nginx.org/packages/ubuntu xenial InRelease    
Hit:19 http://ppa.launchpad.net/otto-kesselgulasch/gimp/ubuntu xenial InRelease
Hit:20 http://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu xenial InRelease
Hit:21 http://ppa.launchpad.net/videolan/master-daily/ubuntu xenial InRelease
Hit:22 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease
Hit:23 https://download.docker.com/linux/ubuntu xenial InRelease
Hit:24 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease
Fetched 408 kB in 3s (117 kB/s)                     
Reading package lists... Done

The situation becomes frustrating when the command is a long one which you have typed. You certainly wouldn’t want to retype that long command. To solve this problem, the following cool linux hack is presented:



Command : sudo !!

This command repeats the previous command with sudo infront. It certainly saves a lot of time and effort and prevents frustration among geeks.

Example:
rohan@Rohan-PC:~$ apt-get update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - 
SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open 
(13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - 
RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - 
RemoveCaches (13: Permission denied)

rohan@Rohan-PC:~$ sudo !!
sudo apt-get update
Hit:1 http://packages.treasuredata.com/3/ubuntu/xenial xenial InRelease
Ign:2 http://dl.google.com/linux/chrome/deb stable InRelease           
Hit:3 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease 
Hit:4 http://archive.ubuntu.com/ubuntu xenial InRelease                
Hit:5 http://dl.google.com/linux/chrome/deb stable Release             
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:8 http://nginx.org/packages/ubuntu xenial InRelease [4, 339 B]      
Hit:9 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease        
Ign:8 http://nginx.org/packages/ubuntu xenial InRelease                
Hit:10 http://ppa.launchpad.net/hanipouspilot/rtlwifi/ubuntu xenial InRelease
Hit:11 https://download.sublimetext.com apt/stable/ InRelease          
Hit:12 http://ppa.launchpad.net/maarten-baert/simplescreenrecorder/ubuntu xenial 
InRelease
Get:13 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Hit:14 http://ppa.launchpad.net/noobslab/apps/ubuntu xenial InRelease  
Get:15 http://archive.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Hit:16 https://deb.nodesource.com/node_8.x xenial InRelease            
Hit:17 https://download.docker.com/linux/ubuntu xenial InRelease       
Hit:18 http://ppa.launchpad.net/noobslab/macbuntu/ubuntu xenial InRelease
Hit:19 http://ppa.launchpad.net/otto-kesselgulasch/gimp/ubuntu xenial InRelease
Hit:20 http://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu xenial InRelease
Hit:21 http://ppa.launchpad.net/videolan/master-daily/ubuntu xenial InRelease
Get:22 http://packages.microsoft.com/repos/vscode stable InRelease [2, 802 B]
Hit:23 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease
Fetched 330 kB in 3s (103 kB/s)                     
Reading package lists... Done



My Personal Notes arrow_drop_up

An enthusiastic Java and web developer with a little affinity for tea, cricket, English, etymology, and reading

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.