Some useful tips while working under GNU Bash – part 2

terminal-icon-512x512[1] Efficient use of command line history using !! and !
Double exclamation i.e, ‘!!’ represents the last run command on the bash. Here is an example :
$ uname -r
3.2.0-55-generic
$ !!
uname -r
3.2.0-55-generic

Now, let’s come to single exclamation i.e, ‘!’ . Unlike ‘!!’, through single exclamation ‘!’, we can access any previously run command that exists in
command line history. Here are some examples :
$ history
   …
   …
   13  exit
   14  uname -r
   15  history
   …
   …
$ !14
uname -r
3.2.0-55-generic

[2] Delete all files in a directory except some (with particular extensions)
Suppose you have a directory with lot of files and you want to delete all the files except some of them (with particular file extensions).
$ ls
1.cpp  find.c        inherit.c   settings.py  spiral.py
2.cpp  helloworld.c  search.php  sort.py

Now, you want to delete all the files except .c and .py files.
$ rm !(*.c|*.py)
$ ls
find.c  helloworld.c  inherit.c  settings.py  sort.py  spiral.py

[3] Make a command not to show up in the output of ‘history’ command
Sometimes you would want to run a command but do not want it to appear in the output of history command. You can achieve this by inserting a space before you type the
command. Note that here I put a space before the ‘df’ command.
$ vim 1.c
$  df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda10      44711000  3709692  38730076   9% /
udev             1968528       12   1968516   1% /dev
tmpfs             792108     1024    791084   1% /run
none                5120        0      5120   0% /run/lock
none             1980260      240   1980020   1% /run/shm
cgroup           1980260        0   1980260   0% /sys/fs/cgroup
. . .
$ history
    1  rm .bash_history
    2  clear
    3  vim 1.c
    4  history
The df command was not captured in the output of history command.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s