How To Run a Previously Entered Command in Linux

By | June 17, 2008

Let’s say you’ve issued a long command and don’t want to type it again. I think you know that all linux commands entered could be shown by issuing history command. History outputs an ordered list of commands that were executed before by current user.

In order to find the command you want, you should enter the following:

history | grep <anything you entered>

This will perform a short search within your command list and will return matching commands with their numbers. Now all you need is to select a command you need and run

!<number of command>

This allows to run the command under this number in such a simple way. You certainly can use up and down arrows to search for a previously entered command, but this will take much more time than using history.

In order to use a previously entered command, enter the following:

!!

This might be dangerous as you cannot be always sure which command was executed before, so better view it before executing.