how to use history command in linux

How to Use Linux History Command

History is a very useful Linux command that allows you to view the most recently executed commands in your current session. It keeps track of all the commands you execute in a given session and enables you to view, reuse those commands quickly. This way you don’t need to retype long and error-prone commands. In this article, we will look at how to use Linux history command.


How to Use Linux History Command

History command is available in almost all Linux shells, however their implementation may slightly differ from one shell to another. For example, in some Linux distributions, history command will also show commands executed from previous sessions.

Open terminal and enter history to view history of recent commands.

$ history

Here is the output

1003  ls
1004  sudo mv tech_blog/ tech-blog/
1005  sudo mv database_blog/ database-blog/
1006  ls
1007  cd analytics-blog/
1008  ls
1009  cd /etc/apache2/sites-enabled/
1010  ls
...
2000  sudo vim .htaccess
2001  history

You will see the each line has a number followed by the command executed. The command number allows you to easily rerun any command quickly, as you will see later.

If you have a large number of commands in your history and want to see only the last 5 commands you have run then use the following command

$ history 5
  1998  cd tech-blog/
  1999  ls
  2000  sudo vim .htaccess
  2001  history
  2002  history 5

Also read : How to Change XAMPP Apache Server Project


Repeating Commands

There are some commands in shell which make use of this history of commands.

For example, here is the command to rerun command number 1009. You just need to enter an exclamation mark followed by the command number you want to repeat.

$ !1009

If you want to repeat the last command you executed, just use double exclamation mark.

$ !!

If you want to repeat the nth previous command (e.g. 10th previous command), then use an exclamation mark, followed by hyphen and the number of previous command, all without any space, as shown below.

$ !-10

Also read : How to Fix Apache Not Executing PHP Files



Search for Command in History

If you want to search for a command from history that starts with a specific string, then type an exclamation followed by your search string, without any spaces.

For example, here is how to search for a command starting with the string “grep”

$ !grep

If you want to search for a command from history that contains a specific string, even if it does not start with it, then type an exclamation, followed by question mark, followed by the search string. Here is an example

$ !?grep

If you want to interactively search your history, just type ctrl+r, you will see a prompt. As you enter your search string, Linux will automatically search the most recent matching command.

(reverse-i-search)`ls': ls -all

Every time you enter ctrl+r, Linux will search for the next matching string going backwards. Once you find the command of your need, press enter to execute it.

Otherwise, press Ctrl+C to exit the interactive search at any time.

Also read : How to Uninstall NGINX in CentOS


Delete Command from History

You can easily delete command from history by adding -d option with history command followed by command number.

$ history 5
  1996  cd tech-blog/
  1997  ls
  1998  sudo vim .htaccess
  1999  history
  2000  history 5
$ history -d 1998
$ history 5
  1997  ls
  1998  history
  1999  history 5
  2000  history -d 1998
  2001  history 5

Also read : How to Set UTF-8 Encoding in Tomcat Server


Modify the Last Command

If you want to modify the very last command, here is how to do it. Let us say in the last command you entered prep instead of grep, then enter the following command to simply replace prep with grep in last command and execute it.

$ ^prep^grep^

Basically you have to enter ^ followed by old string, followed by ^, followed by new string and finally ^ again.

That’s it. We have given a basic overview of how to use Linux history command. As you can see, history command is a very useful tool to be more efficient, and also helps you avoid re-typing commands unnecessarily.

Also read : How to Delete or Empty Large Files in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *