Linux Blog

Linux History Command

Filed under: General Linux,Linux Software — TheLinuxBlog.com at 5:00 am on Wednesday, November 17, 2010

linux find command

History is great. How does the saying go?

“Those who forget about history are doomed to repeat it?”

If that’s the saying I think it is more fitting to say that for those who forget the Linux History Command are doomed to repeat typing. A lot. Seriously, the history command can help you remember the exact Linux find command with the intricate search options you typed a while ago. It could help you open up your x2x or x2vnc sessions after a reboot. Who knows what you’ll use it for. All this comes at a little cost, you’ll have to know how to use it.

Basically you just do everything as normal except when you want to get rid of history, say because you did a whois on that dirty domain name just to see if it was available. Not saying I’ve ever done that or anything though.

To do that you just do:

history -c

Before you go and erase all those embarrassing wgets you did of <insert your favorite celebrity here> If you run the history command with no options you will see everything you’ve done with a number beside it. Go ahead and do it now. Once you’ve done that pick a command, one that you’ll know wont cause any damage. Perhaps an ls. If the number beside it is 1337 type the following:

!1337

That’s exclamation 1337. This will cause the Linux History Command to repeat the command specified at 1337 of the history file. Apart from writing this, I’ve only used this a handful of times. It mostly comes in handy when you’re trying to create one line shell scripts.

You can repeat the last command in the history with a double exclamation “!!”

To view all of your history in a more friendly format you can use

history | more
history | less

using more with history doesn’t give you any more history. Its just a pager. Less as you guessed doesn’t give you less although I know plenty of high school students that wish it would.

sing the Linux History Command with the tail command doesn’t search for the history of the pictures of the hot tail you saved, but rather the last things in your history, or any number, starting from the bottom up. If you want to see the last 20 things you put in history do this:

history | tail -n 20

I don’t think I can to make a pun for head. Oh wait, I’ve got one. It does the exact same thing as tail, except it goes down on history. You can also do it from the head down by doing:

history | head

If you want to, you can also view the history in you’re favorite text editor by editing the .bash_history. For example:

vim .bash_history</p>
<p>If you want to find out if you've used the wrong text editor in the past do this:</p>
<pre lang="bash">history | grep emacs

If you see anything other than the command you just typed, you’re using the wrong text editor. Oh, that’s right. I went there. Again (Handy Linux Wallpaper).

Man Pages for commands in this post »

less
history
more
head
tail
whois

2 Comments »

Comment by tommyboy

December 2, 2010 @ 7:17 pm

LOL! Last command is great, i hate emacs!

Comment by gkokmdam

December 22, 2010 @ 4:45 pm

Thanks for telling all this!

Make sure (in bash-shell) to also search in History using Ctrl-R and then start typing just a few chars from a line you used before. That should speed things up.

And have you ever had to use that last commandline argument again and again? (like mkdir xyz / cd xyz) Start typing cd and the hit ESCAPE and then ‘.’ This will add the previous last commandline argument to your current command-line. How I love bash!

(I know, this was not exhaustive, come, please teach me more!)

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>