How To Search Shell Command History

by on March 6, 2008 · 13 comments· LAST UPDATED March 6, 2008

in , ,

Q. How do I search old command history under bash shell? How do I display or modify previous commands?

A. Almost all modern shell allows you to search command history if enabled by user. Use history command to display the history list with line numbers. Lines listed with with a * have been modified by user.

Shell history search command

Type history at a shell prompt:
$ history
Output:
Sample output:

  6  du -c
    7  du -ch
    8  ls [01-15]*-2008
    9  ls -ld [01-15]*-2008
   10  ls -ld [1-15]*-2008
   11  ls -ld [0]*-2008
   12  ls -ld [01]*-2008
   13  rm -vrf [01]*-2008
   14  du -ch
   15  ls
   16  cd
   17  umount /mnt
   18  df -H
   19  vnstat
   20  yum update
   21  vnstat -m
   22  vnstat -m -i eth0
....
...
  996  ping router.nixcraft.in
  997  ssh vivek@p1.vpn.nixcraft.in
  998  alias
  999  ~/scripts/clean.rss --fetch
 1000  vnstat
 1001  ~/scripts/clean.rss  --update

To search particular command, enter:
$ history | grep command-name
$ history | egrep -i 'scp|ssh|ftp'

Emacs Line-Edit Mode Command History Searching

To get previous command containing string, hit [CTRL]+[r] followed by search string:

(reverse-i-search): 

To get previous command, hit [CTRL]+[p]. You can also use up arrow key.

CTRL-p

To get next command, hit [CTRL]+[n]. You can also use down arrow key.

CTRL-n

fc command

fc stands for either "find command" or "fix command. For example list last 10 command, enter:
$ fc -l 10
To list commands 130 through 150, enter:
$ fc -l 130 150
To list all commands since the last command beginning with ssh, enter:
$ fc -l ssh
You can edit commands 1 through 5 using vi text editor, enter:
$ fc -e vi 1 5

Delete command history

The -c option causes the history list to be cleared by deleting all of the entries:
$ history -c

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 13 comments… read them below or add one }

1 senthil March 7, 2008 at 9:07 pm

hopefully looking for some tricks

Reply

2 Binny V A March 9, 2008 at 5:45 pm

To run a command whose number you know…
!203

Runs the 203rd command in history

Reply

3 Cuetz March 10, 2008 at 5:23 pm

You can press + and type some command:

For example

‘+ cd’

immediately apear the last command that match with ‘cd’ if you press + again, you can brows trough all the cd commands you has entered in your history

Reply

4 Addy October 28, 2010 at 6:26 am

this dint work on ubuntu 4 me!!

Reply

5 Naresh September 7, 2009 at 9:25 am

These r really helpful to every Linux user.

Reply

6 Amy August 31, 2010 at 10:52 am

This acts as a guide for us… :-)

Reply

7 raja kashif February 18, 2011 at 9:48 am

plz tell me that how we a order make

Reply

8 M.S. Babaei April 7, 2011 at 8:31 pm

How to clear history in sh?
Does fc command clears history?

Reply

9 chaitanya October 7, 2011 at 3:03 am

how to know the full history of shell from the starting of first day

Reply

10 Anupama August 31, 2012 at 3:17 am

Is there a way to retrieve command history in a shell script?

Reply

11 Ashish Singhi September 10, 2012 at 12:27 pm

No. Within a script, the history mechanism is disabled.

Reply

12 Rukki September 17, 2012 at 1:26 pm

How to recover cleared history again..??
I mean after pressing history -c..Please Help Me.:)

Reply

13 Frank Kim July 7, 2014 at 2:00 pm

Thanks. Awesome summary.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: