Unix – Executing Commands in Your History

The Unix $ history command lists the last 500 commands that have been executed in the Shell and is a very useful tool for remembering and re-running commands that have been executed recently. It is rare to need all 500 commands, so the history command works best when the output is limited to a certain number of results or the standard output of the history command is used as standard input for the grep command.

The following commands can be used to get the last 10 shell commands.

$ history 10
$ history | tail

Use the following command to find all commands that have been executed recently with the word rails.

$ history | grep rails

The output from this command, will look something like this:

$ history | grep rails
  508  rails g model Student name age:integer
  509  rails g model Course name credits:float
  510  rails g migration courses_students student_id:integer course_id:integer

Each historical command is numbered and it is simple to re-execute these commands with these numbers. For example, to re-execute command number 509 (rails g model Course name credits:float), simply enter the following command:

$ !509

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s