Useful Bash Commands

Overview

NOTE!: If you feel some commands are missing in this post,
Feel free to add those commands in below comments box.

Shortcuts

/---------------------------------------------------------------------------------------\
|	!!	Repeat last command							|
|	!char	Repeat last command That started with char				|
|	!num	Repeat a command by its number in history output			|
|										        |
|	!-n	Repeat a command entered n command back					|
|	!?abc	Repeat last command that contains (as opposed to ?started with?) abc    |
\---------------------------------------------------------------------------------------/
/---------------------------------------------------------------\
|	UP DOWN Keys	Scroll through previous commands	|
|	Ctrl+r		Reverse-i-search		  	|
\---------------------------------------------------------------/

Clear Terminal

# Linux/Unix OS
$ <CTRL+l>
# MAC OS
# The following keys not just clear screen but also clear Terminal buffer
# After <COMMAND+k> you can't able to view previous buffer
$ <COMMAND+k>

Run Previous Command With Root/Sudo Privilege

$ sudo !!

Run Previous Command With Search/Replace For First Instance

$ echo "Hello Mitesh, Hello Shah, Hello Visitor"
Hello Mitesh, Hello Shah, Hello Visitor
$ ^Hello^Hi
$ echo "Hi Mitesh, Hello Shah, Hello Visitor"
Hi Mitesh, Hello Shah, Hello Visitor

Run Previous Command With Search/Replace For All The Instance

$ echo "Hello Mitesh, Hello Shah, Hello Visitor"
Hello Mitesh, Hello Shah, Hello Visitor
$ !!:gs/Hello/Hi
$ echo "Hi Mitesh, Hi Shah, Hi Visitor"
Hi Mitesh, Hi Shah, Hi Visitor

Execute Command Without Saving in History

  • Prepending one or more spaces to your command won’t be saved in history.
$ <SPACE><COMMAND>

NOTE!: HISTCONTROL=ignorespace will ignore just the commands that begin with a space.
Use HISTCONTROL=ignoreboth if you also want to ignore duplicates.

Paste Last Argument of Most Recent Command

/-----------------------------------------------------------------------\
|	Esc .		The escape key followed by a period.		|
|	Alt+.		Hold down alt key while pressing the period.	|
|	!$		Only valid for last argument			|
\-----------------------------------------------------------------------/
$ cp -v index.html /home/mitesh/shah/edytk.github.io/index.html
# Using ALT Dot
$ cd <ALT+.>
$ cd /home/mitesh/shah/edytk.github.io/index.html
# Using Esc Dot
$ cd <ESC+.>
$ cd /home/mitesh/shah/edytk.github.io/index.html
# Using !$
$ cd !$
$ cd /home/mitesh/shah/edytk.github.io/index.html

Paste Selected Argument

  • This comes in handy when you’ve written a long command with multiple arguments and you want to reuse one of them.
# Use Only 3rd Argument
$ echo a b c d e
a b c d e
$ echo !:3
echo c
c

# Used 1-3 Argument Only
$ echo a b c d e
a b c d e
$ echo !:1-3
echo a b c
a b c

# If you leave off the first digit and just start with `!:-`
# then you'll include argument 0, which is the previous command.
$ echo a b c d e
a b c d e
$ echo !:-3
echo echo a b c
echo a b c

Reset Terminal

  • Sometime we send binary output to the terminal, Which make terminal not useable.
  • The reset command reset your terminal, Note that when you type reset its won’t properly echo back on terminal.
$ reset

Display Mount Filesystem in Nice Layout

$ mount | column -t
/dev/root  on  /                         type  ext4         (rw,relatime,errors=remount-ro,data=ordered)
devtmpfs   on  /dev                      type  devtmpfs     (rw,relatime,size=4037132k,nr_inodes=1009283,mode=755)
none       on  /dev/pts                  type  devpts       (rw,nosuid,noexec,relatime,mode=600)
none       on  /proc                     type  proc         (rw,nosuid,nodev,noexec,relatime)
none       on  /sys                      type  sysfs        (rw,nosuid,nodev,noexec,relatime)
none       on  /proc/sys/fs/binfmt_misc  type  binfmt_misc  (rw,nosuid,nodev,noexec,relatime)
none       on  /sys/fs/fuse/connections  type  fusectl      (rw,relatime)
none       on  /sys/kernel/security      type  securityfs   (rw,relatime)
none       on  /run                      type  tmpfs        (rw,nosuid,noexec,relatime,size=807520k,mode=755)
none       on  /run/lock                 type  tmpfs        (rw,nosuid,nodev,noexec,relatime,size=5120k)
none       on  /run/shm                  type  tmpfs        (rw,nosuid,nodev,relatime)

ASCII Table

$ man ascii

Compare Remote File with Local File

$ ssh USERNAME@HOSTNAME cat /path/to/remotefile | diff /path/to/localfile -

Decide Which Command Run On Success and Fail

$ cal && echo "Right Command" || echo "Wrong Command"
July 2015
Su Mo Tu We Th Fr Sa  
     1  2  3  4  
5  6  7  8  9 10 11  
12 13 14 15 16 17 18  
19 20 21 22 23 24 25  
26 27 28 29 30 31

Right Command

$  call && echo "Right Command" || echo "Wrong Command"
No command 'call' found, did you mean:
 Command 'wall' from package 'bsdutils' (main)
 Command 'calc' from package 'apcalc' (universe)
 Command 'cal' from package 'bsdmainutils' (main)
call: command not found

Wrong Command

Execute Command After Every 2 Seconds

$ watch -d df -h
Every 2.0s: df -h                                                                                                                      Tue Jul  7 13:52:39 2015

Filesystem      Size  Used Avail Use% Mounted on
rootfs          500G  250G  250G  50% /
/dev/root       500G  250G  250G  50% /
devtmpfs        3.9G  4.0K  3.9G   1% /dev
none            789M  165M  625M  21% /run
none            5.0M     0  5.0M   0% /run/lock
none            3.9G     0  3.9G   0% /run/shm

Check Last Command Status

  • If command run Successfully then exit status is zero
  • If command doesn’t run Successfully then exit status is non-zero
$ cal ; echo $?
$ call ; echo $?


Newsletter

Get updated when I create new content.
Unsubscribe whenever. Never any spam.

Note

I'm Tama. I turn down every ad, affiliate, and sponsor request I get. I write free resources that help people. If you enjoy my content, please consider supporting what I do.

Support my work