How To Search Shell Command History

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.

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
18 comments… add one
  • senthil Mar 7, 2008 @ 21:07

    hopefully looking for some tricks

  • Binny V A Mar 9, 2008 @ 17:45

    To run a command whose number you know…
    !203

    Runs the 203rd command in history

  • Cuetz Mar 10, 2008 @ 17:23

    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

    • Addy Oct 28, 2010 @ 6:26

      this dint work on ubuntu 4 me!!

      • mulepest Apr 9, 2015 @ 0:46

        didn’t wont on mint either

  • Naresh Sep 7, 2009 @ 9:25

    These r really helpful to every Linux user.

  • Amy Aug 31, 2010 @ 10:52

    This acts as a guide for us… :-)

  • raja kashif Feb 18, 2011 @ 9:48

    plz tell me that how we a order make

  • M.S. Babaei Apr 7, 2011 @ 20:31

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

  • chaitanya Oct 7, 2011 @ 3:03

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

  • Anupama Aug 31, 2012 @ 3:17

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

    • Ashish Singhi Sep 10, 2012 @ 12:27

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

  • Rukki Sep 17, 2012 @ 13:26

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

  • Frank Kim Jul 7, 2014 @ 14:00

    Thanks. Awesome summary.

  • Openies Jan 16, 2015 @ 6:01

    make a entry of

    hgrep () {
    history | egrep –color=auto –recursive “$@”
    }

    In .bashrc file.

    it will allow you to search history like

    history ssh

    output:
    21 ssh openerp@odoo.openies.com
    22 ssh openerp@odoo.openies.com
    26 ssh root@odoo.openies.com

  • Mohammad Shahid Feb 5, 2016 @ 9:42

    its really very helpful, Thanks yaar

  • Jim Mar 19, 2016 @ 19:18

    To get history in a shell script, I’ve had to resort to this:
    alias cmd=’history | shell_script’
    Then I just create a function:
    function shell_script {
    }
    and history is available.

    Another approach is to read the history file:
    grep somepattern $HISTFILE
    However, its contents will be unreliable if you are running multiple shells.

    I usually search command history with this alias:
    alias gh=’history|grep’

  • Jim Mar 19, 2016 @ 19:20

    I misspoke….
    Either I create an alias that pipes history into the shell command
    OR
    I create a function() that has access to the current history – the function could be the entire script, or it could pipe the command history, possibly filtered, to the script.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.