Linux history Command Tutorial for Beginners (8 Examples)

Linux history Command

Linux history Command,If your work entails jogging tools and scripts on the Linux command line, I am certain there are loads of instructions you will be running each day. Those new to the command line must recognize there exists a tool – dubbed records – that offers you a list of instructions you have finished in advance.

In this tutorial, we will discuss the basics of the history command the use of some smooth to understand examples. But before we do that, it’s well worth citing that each one examples right here were examined on an Ubuntu sixteen.04LTS machine.

Linux history command

If you know how to efficiently make use of your command line history, you can keep a whole lot of time on every day basis. Following are some Q&A-styled examples that ought to give you an excellent concept on how you could use the records command in your advantage.

Linux history Command,Q1. How does history command work?

Basic usage is simple – just execute the ‘history’ command.


history
How does history command work

So you could see your previously run instructions are shown inside the output. Note that I actually have used the ‘extra’ command to ensure the output display is paused at the primary web page (the complete output of ‘records’ is generally very long).

Linux history Command,Q2. How to make history display date and time as well?

If you want the records command to display the date and time of execution for each command, you could do this through exporting the HISTTIMEFORMAT variable inside the following manner:


export HISTTIMEFORMAT="%F %T: "

That’s it. Now when you’ll run the ‘history’ command, you’ll see the date and time info in the output.

How to make history display date and time as well

In case you see date and time to be the same for all commands, the following explanation should make things clear:


If you set the HISTTIMEFORMAT in bash your new entries get stored in the history file with a
timestamp, older commands that don't have a timestamp (those before you ever set HISTTIMEFORMAT
will display one and the same date-time-stamp (I assume the one from the first entry found with a
real timestamp).

This problem should solve itself after your complete history has been updated in a few days.

You can look in ~/.bash_history to see what is the first line that has a date-time-stamp.
Those are lines starting with a # followed by a (currently) 10 digit number.

Linux history Command,Q3. How to make history stop recording commands?

If a situation arises wherein you do now not want the system to shop your instructions history, then you can do this the usage of the HISTSIZE variable. Just export it with zero as its price, and you’re proper.

See the following instance:

How to make history stop recording commands

So you may see, going for walks the ‘history’ command now did not produce any consequences.

Note that there is also a HISTFILESIZE variable. Here’s the difference between the 2:


HISTSIZE is the number of lines or commands that are stored in memory in a history list while your
bash session is ongoing.

HISTFILESIZE is the number of lines or commands that (a) are allowed in the history file at startup
time of a session, and (b) are stored in the history file at the end of your bash session for use in
future sessions.

Q4. How to make history not record a single command?

This can be done by setting the HISTCONTROL variable to ‘ignorespace.’ This you can do in the following way:


export HISTCONTROL=ignorespace

Now on every occasion you run a command that you don’t want to be recorded in history, simply execute it with a main space.

Of route, you can use the grep command to look for specific keywords or command within the output produced by using the records command. However, there also exists a miles quicker method: reverse-i-seek.

It’s pretty clean – all you have to do is to press CTRL+R at the same time as on the command line, and also you see your activate becoming some thing like this:

Now type the keyword you need to search, and you will see matching recommendations from records at the proper. For example, I typed ‘man’ (with out rates), and the first end result I were given became ‘man apropos’.

If this is not what you are seeking out, you could get the following matching result through urgent CTRL+R once more. So in my case, the following end result I were given became ‘guy discover seek.’

You can maintain on searching this manner. Once you get the end result you need, just press the right arrow key and you may get the complete command in your prompt.

So now you may run this command, or use it any way you need.

Q6. How to run the previous command?

There are clean ways in which you can run the last performed command: press CTRL+P, or run the following command:


!!

Don’t believe (particularly the second manner)? Try it.

In case the requirement is to quickly execute the second remaining command, the you may do this in the following manner:


!-1

So on and so forth.

Q7. How to make history ignore duplicate entries in output?

This may be performed via putting the HISTCONTROL variable to ‘ignoreboth’ and ‘erasedups.’ You can do it within the following manner:


echo HISTCONTROL=ignoreboth:erasedups

Now when you may run the records command, the output may not incorporate duplicates.

Q8. How to clear history?

If you want to clean current history so you can start afresh, use the -c command line option.


history -c