Using the linux commands effectively can speed up your workflow. Below are 10 useful linux commands with practical examples.


1. Go back to prior directory

You type a long directory and you want to go back to prior directory use cd -

user@server.com: /etc $
user@server.com: cd /somepath/long_directory. 

user@server.com: cd -

2. Clear screen / prior entries

Use Ctrl + L to clear prior entries. The old commands are still visible if we scroll up. To remove all entries type reset this will reset the shell.


3. Perform minimize an app function

Say you are editing a file in vi and you want to come back to it but not save it. To do this you can minimize the press Ctrl + Z to bring the program to background.

To bring it back use fg to bring it to foreground.

Think of this like minimizing an app in a GUI based application but this is for the terminal.

user@server.com: vi /etc/ssh/sshd_config

# Enter Ctrl + Z to bring to background
[1]+  Stopped                 vi /etc/ssh/sshd_config

user@server.com: fg

4. Repeat the last command

Some commands require sudo and we may miss to update initially. To repeat the prior command we can use !! below we used sudo !! to repeat the last command instead of typing it all again.


user@server.com: apt update
Reading package lists... Done
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)

user@server.com: sudo !!

5. Remember partial command

You remember parts of a partial command to search it use Ctrl + R this opens a search box. Type the partial text and keep hitting Ctrl + R till the required command is found

user@server.com:
# Hit Ctrl + R
(reverse-i-search)`sudo': sudo apt update

6. See past history of command and run it

Enter history this shows all command that was run with a number next to it. To execute a command you can use ! followed by the number

e.g. to re-execute the sudo apt update we can do !261 this will re-run the command again.

user@server.com: history
  260  apt update
  261  sudo apt update
  262  history

user@server.com: !261

if you want date and time of the command when it was ran add the following to your .bashrc or .zshrc file so its persistent in any terminal.

HISTTIMEFORMAT="%Y-%m-%d %T "

next time you run history it will show the timestamp of the command when ran in YYYY-MM-DD HH:MM:SS format.


7. Keyboard shortcut to move in commands

Say you enter a long command and you want to navigate without hitting arrow keys. Use

  • Ctrl + A to goto the beginning of command
  • Ctrl + E to goto the end of command
  • Ctrl + U to clear the command

8. Use && to chain command

If we want to run two commands use && to chain the command. The && ensures that the next command is run only if the prior command runs successfully

user@server.com: sudo apt update && sudo apt upgrade -y

9. Truncate a file

Say you are testing a file and you want to clear all entries in the file instead of doing rm <file_name> you can use truncate here -s is size to make it 0 bytes.

Please be careful when using truncate and always specify the file name

user@server.com: truncate -s 0 sample.txt

10. Use column to view output

Use column to format to multiple column. Helps to view the output better.

Here -t is table format and -s is separator as comma

Column can be used with other commands where we want to see the output in column or table format

user@server.com: cat sample.txt
S.No,Name
1,A
2,B
3,C

user@server.com: cat sample.txt | column -t -s ","
S.No  Name
1     A
2     B
3     C