Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 2, 2020 06:48 am GMT

History Command and How to Use It?

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

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

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

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

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

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

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.

CommandDescription
!!It refers to the last command
!-nIt will execute the current number -n command
!stringIt will execute the command referring to the most recent command starting with input string
!?stringIt will execute the most recent command that ends with the input string
^string1^string2Repeat 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

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

history-hello

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 :)


Original Link: https://dev.to/yashsugandh/history-command-and-how-to-use-it-37k8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To