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.

Shell history search command

Type history at a shell prompt:
$ history
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
  997  ssh
  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:


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


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


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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts