Within a terminal, when you press up in your keyboard, you get to see the past commands you have given to it before in order. I was wondering if there was a way to see only the commands in the past starting with -for instance, "ls -l". That way, if the list of commands is:

  1. ls -l -a
  2. cmake
  3. cd ~/Desktop
  4. cmake

A way to just write "ls" and tab or whatever combination of key-strokes needed to pop up the past command giving in to the terminal starting with "ls"


5 Answers 5


It won't show things only starting with ls, but you can do Ctrl-R ls to do a backwards search through history. Repeat Ctrl-R to find the next match.

  • Thank you for the CTRL+R shortcut command.
    – Kleber S.
    Commented Dec 7, 2012 at 11:34

Sometimes it's useful to have this...


Connecting to another machine using ssh:

start typing


Use PgUp key and your ssh ends up with your last used ... press PgUp again and it gives you the next one and so on. Like it?

You can use this:

Edit your .bashrc (vi /home/<yourfolder>/.bashrc) and add this:

$if Bash
    # Search history back and forward using page-up and page-down
    "\e[5~": history-search-backward
    "\e[6~": history-search-forward

    # Completion
    set match-hidden-files off
    set page-completions off
    set completion-query-items 350
    set show-all-if-ambiguous on


This adds only to your user's terminal this function. You will have to edit also the one of the root to have the same: you can follow this tutorial from the wiki there's no way you can do it wrong, also there is a lot of extra stuff to improve your terminal experience...

  • You can even bind it to the Up/Down arrow keys: bind '"\e[A": history-search-backward'; bind '"\e[B": history-search-forward', making it even more intuitive.
    – Fritz
    Commented Jun 25, 2019 at 11:35

To see the command history, use can use:

$ history

If you only want to see the past command begin with for example "ls", in my opinion I uses:

$ history | grep ls

Certainly the output will contain something not a command but contain "ls" characters, but I think it can help.


The easiest way to achieve what you want is by !<whatever-command>. For example to execute the ls that you performed last, you can do a !ls. Take a look at the following log to see if this is what you need:

nits@nits-excalibur:~$ ls /mnt/Storage_1/Music/
Music I  Music II  Music III  Music IV  Music V  Music_VI
nits@nits-excalibur:~$ cd Documents/
nits@nits-excalibur:~/Documents$ top
nits@nits-excalibur:~/Documents$ !ls
ls /mnt/Storage_1/Music/
Music I  Music II  Music III  Music IV  Music V  Music_VI
  • 1
    This runs the command without displaying it, is there an alternative that displays it as the OP requested? Commented Feb 27, 2012 at 10:32
  • In zsh, you can setopt HIST_VERIFY and the command will be put in your input buffer, where you can either press enter to accept it, or edit it.
    – poolie
    Commented Feb 27, 2012 at 10:35

The history is located ~/.bash_history Also, for a neat trick to search your bash history press ctrl+r then type the start of the command you're looking for.

and http://ubuntuforums.org/showthread.php?t=204382 for Handy command-line aliases and tricks

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .