Linux and history command – How to save time

Hello all, one of the things I love more about Linux is that you can do everything easily and sometimes saving a lot of time and typing, this is specially important when you’re preparing for a performance (and time) based exam like RHCE.

Today I want to talk about a great time saver which is the history command and feature built-in the bash shell of Linux.

I think you all know that every time you input a command into a shell Linux places that command in a special system variable which is, surprise, called HISTORY. This “buffer” is used to hold in memory a finite, but configurable, number of commands the administrator or the user has used over time just to give you an idea this is the partial output of the history command on one of my lab machines :

[ccielogs@lethe ~]# history...  110  ll  111  cat newpasswd   112  sed 's/Test/test/g' /etc/passwd > newpasswd  113  ll  114  cat newpasswd   115  w  116  who  117  ll ; w  118  w ; ll  119  history   120  history 114  121  cat newpasswd   122  rm newpasswd   123  cat /etc/passwd  124  vi /etc/passwd  125  cat /etc/passwd  126  clear  127  history...[ccielogs@lethe ~]#

 

As you can see my Linux system registered a lot of the commands I’ve typed so far, this good both for reference and for taking advantage of its “do it quickly” approach as, for example, I was to launch again the command at line 112 all I have to do is to write the following :

[ccielogs@lethe ~]# !112

 

And automatically Linux would execute the command at line 112 (sed ‘s/Test/test/g’ /etc/passwd > newpasswd in this example), I think it’s clear how much time this can save you! Ok but what if I want to execute just the last command I’ve typed in? Yes I know you could use the up arrow to recall the last command, but an alternate way to do this is typing a double exclamation mark, that is !!, and Linux automatically will launch the last command,.

But there is more, consider the following output from the history command :

[ccielogs@lethe ~]# history  128  pwd  129  pwd  130  pwd  131  history[root@lethe ~]# 

Do you notice anything weird here? Yes Linux, by default, won’t take care about duplicated commands into its HISTORY variable. This can be easily modified with a few simple steps. First you could simply launch the following command :

[ccielogs@lethe ~]# export HISTORY=ignoredups

 

Ok now every time I input a command that is already in the shell’s history it’ll get ignored. If you want to make this change available to all users, or system wide if you prefer, just modify the .bashrc file so with the same line (expect the "export” command of course) but I’ll cover this in another post.

I hope you’ll find this post informative and useful if so please digg it to make available to even more people. If you face any problem or have any question don’t hesitate to comment or to write me with your questions that I’ll be glad to answer.

Lethe.

Buzz it!
  • Share/Bookmark

2 Comments

Trackbacks and Pingbacks

Leave a Reply

 

Get Adobe Flash playerPlugin by wpburn.com wordpress themes