If your work involves running tools and scripts on the Linux command line, I am sure there are a lot of commands you would be running each day. Those new to the command line should know there exists a tool – dubbed history – that gives you a list of commands you’ve executed earlier.
In this tutorial, we will discuss the basics of the history command using some easy to understand examples. But before we do that, it’s worth mentioning that all examples here have been tested on an Ubuntu 16.04LTS machine.
Linux history command
If you know how to effectively utilize your command line history, you can save a lot of time on daily basis. Following are some Q&A-styled examples that should give you a good idea on how you can use the history command to your benefit.
Q1. How does history command work?
Basic usage is simple – just execute the ‘history’ command.
So you can see your previously run commands are shown in the output. Note that I have used the ‘more‘ command to make sure the output display is paused at the first page (the complete output of ‘history’ is usually very long).
Q2. How to make history display date and time as well?
If you want the history command to display the date and time of execution for each command, you can do this by exporting the HISTTIMEFORMAT variable in the following way:
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.
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
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.
Q3. How to make history stop recording commands?
If a situation arises wherein you do not want the system to save your commands history, then you can do this using the HISTSIZE variable. Just export it with zero as its value, and you’re good.
See the following example:
So you can see, running the ‘history’ command now didn’t produce any results.
Note that there’s also a HISTFILESIZE variable. Here’s the difference between the two:
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
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:
Now whenever you run a command that you don’t want to be recorded in history, just execute it with a leading space.
Q5. How to perform quick history search?
Of course, you can use the grep command to search for specific keywords or command in the output produced by the history command. However, there also exists a much faster method: reverse-i-search.
It’s quite easy – all you have to do is to press CTRL+R while on the command line, and you see your prompt turning into something like this:
Now type the keyword you want to search, and you’ll see matching suggestions from history on the right. For example, I typed ‘man’ (without quotes), and the first result I got was ‘man apropos’.
If this is not what you are looking for, you can get the next matching result by pressing CTRL+R again. So in my case, the next result I got was ‘man find search.’
You can keep on searching this way. Once you get the result you want, just press the right arrow key and you’ll get the whole command on your prompt.
So now you can run this command, or use it any way you want.
Q6. How to run the previous command?
There are two easy ways in which you can run the last executed command: press CTRL+P, or run the following command:
Don’t believe (particularly the second way)? Try it.
In case the requirement is to quickly execute the second last command, the you can do that in the following way:
So on and so forth.
Q7. How to make history ignore duplicate entries in output?
This can be done by setting the HISTCONTROL variable to ‘ignoreboth’ and ‘erasedups.’ You can do it in the following way:
Now when you’ll run the history command, the output won’t contain duplicates.
Q8. How to clear history?
If you want to clear existing history so that you can start afresh, use the -c command line option.
You’ll likely agree that history is a very important command that you should be aware of. In this tutorial, we discussed several important tips/trick to get the best out of this tool. Once you’ve practiced these, head to the tool’s man page for more info.
Source : HowToForge