Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 12 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

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

share|improve this answer

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

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 for Handy command-line aliases and tricks

share|improve this answer

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.