Take the 2-minute tour ×

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"


share|improve this question
add comment

5 Answers

up vote 7 down vote accepted

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.

share|improve this answer
Thank you for the CTRL+R shortcut command. –  Kleber S. Dec 7 '12 at 11:34
add comment

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

    # 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...

share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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
share|improve this answer
This runs the command without displaying it, is there an alternative that displays it as the OP requested? –  Bruno Pereira Feb 27 '12 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 Feb 27 '12 at 10:35
add comment

Your Answer


