History Command and How to Use It?
Tags: ubuntu, linux, beginners, computerscience
Published: 2020-08-02 06:48:17

In this series, we have looked at so many commands with their different syntaxes and options, and it's very very hard to remember each and every one of them.

So, today we are going to have a look at a command which allows us to have a look at all the commands that we have used previously and use them again.

By the title, you would have already guessed what the command is, right?

It's `history` command!!

The first question we have is, what is the use of `history` command?

The `history` command is used to view all the previously executed commands. 

Why do we need history command?

The main use of history is to find an already executed command and use it again.

It can also be used for audit purposes or to find out what command was executed at a specific date and time.

Now, without wasting any more time let's get started 

- To get all the commands we have previously executed 

![history basic](https://dev-to-uploads.s3.amazonaws.com/i/vtwb1695wamd0q64kd7s.png)

To use the `history` command we simply type history and press ENTER

As soon as we press ENTER we get the list of all the commands.

By default, the bash used to store around **500 commands** in the history but nowaday's the limit has gone up to **1000 commands**.

To make the scrolling of commands a little easier we can use the `history` command along with the `less` command i.e. `history | less`.

But, what if we wanted to find only the last 10 commands

- List the last 10 commands that we executed

![history-last-10-updated](https://dev-to-uploads.s3.amazonaws.com/i/n8afjxdmsjiy98owdmct.png)

In the above example, we used the command `history` along with the number of commands we wanted to have a look at.

**Note:** The above syntax i.e. `history {number}` will not work with **zsh** because in **zsh**, `history` is an alias for `fc -l 1`, so when you do `history 10` it gets replaced by `fc -l 1 -10` which just won't work, so instead use `fc` directly: `fc -l -10`.

Now, what if we want to list them along with the time

![history-with-time](https://dev-to-uploads.s3.amazonaws.com/i/gcwtzkziv3rr9mje6j51.png)

In the above example, we first used the command
`$ HISTTIMEFORMAT="%d/%m/%y %T "` which is used to set the time format day/month/year timestamp
After the format is set we used the command `history` to get the desired results.

**Note:** For **zsh** we just need to use `history -i` to get the same results.

What if we wanted to find the history of all the times we used a specific command?

![history-with-grep](https://dev-to-uploads.s3.amazonaws.com/i/jnyzklrddbjy2yp49fkv.png)

In the above example, we used the command `history| grep man` to get all the times we have used the `man` command

Till now we have seen different way's to get the commands.

But what if we wanted to use these commands.

For starters, what if after looking through history we found out the command we want to use has the x serial number. How will we use it?

So let's see how we can use the commands from our history 

- use a command on the serial number **1343**

![history-serial](https://dev-to-uploads.s3.amazonaws.com/i/8xwijsb36ytohkybpnhm.png)

In the above example, we used `!1343` to execute the command at the respective serial number.

But this can land us into a lot of trouble if we typed the wrong serial number.

Is there anything we can do to prevent this?

Let's take an example to see how to prevent this

![history-serial-check](https://dev-to-uploads.s3.amazonaws.com/i/nym99bj389jxrshy7507.png)

In the above example, we first used the command `!1343:p` which returns the command at the number and after verification, we can execute the command.

There is another feature that history provides known as **History Expansion**

History expansion is a way to **recall** and **edit commands** in the history list. The way to recall commands is by the use of **event designators**.

| Command          	| Description                                                                                   	|
|------------------	|-----------------------------------------------------------------------------------------------	|
| !!               	| It refers to the last command                                                                 	|
| !-n              	| It will execute the current number -n command                                                 	|
| !string          	| It will execute the command referring to the most recent command starting with input string  	|
| !?string         	| It will execute the most recent command that ends with the input string                      	|
| ^string1^string2 	| Repeat the last command, replacing  string1 with string2                                      	|

We can also use `!!` to execute the last command but we would rarely use it since we can easily use the `up` key instead.

Let's take a few example's to understand better

- find the last `cat` command that was executed and run it

![history-cat](https://dev-to-uploads.s3.amazonaws.com/i/e34nz7v3zgbwp52lb0os.png)

- find the last command performed on "Hello.txt" and perform it again

![history-hello](https://dev-to-uploads.s3.amazonaws.com/i/c6udyjnkzpdif7gnqyci.png)

Similarly, we can use other history expansions as well.

So, this was all about the history keyword and how we can use it.

Hope you understood it and please let me know if there are questions and suggestions down below.

See you in the sunny papers :)