Home Terminal tips and tricks
Post
Cancel

Terminal tips and tricks

Introduction

When it comes to linux terminals I have been fortunate enough to have come across many tips and tricks from many of my mentors, colleagues and as well as the internet(Stack Overflow obviously).

The first one is my personal favourite and is one that everybody badly needs, have you ever run a command and then get the most common error that you do not have permission or you need to be root only to realise you haven’t used sudo and then you end up either retyping the command like sudo <your-command> or you press the up arrow and then keyboard shortcut home button and then type sudo to end up with sudo <your-command> What if you could just use sudo bang bang instead, a cooler and much efficient way to re-run immediate previous command but with modifications, in our context adding the sudo to the beginning.

Ex: When you run the below

1
ls /root

Output

ls: cannot open directory '/root':

So, run the below command where !! will append the previous run command.

1
sudo !!

Output:

sudo ls /root
[sudo] password for ubuntu:

Reverse lookup on history (commands that were run previously)

Ctrl+r and type parts of previously used command. For example, to get a previously run ssh command

Key in Ctrl=r and start typing

1
(reverse-i-search)`ssh': ssh -i ~/.ssh/id_rsa root@10.0.0.2

Exit a session using exit command or shortcut Ctrl+d

1
exit

or use Ctrl-d


This post is licensed under CC BY 4.0 by the author.