Submitted by drupalmaster on

While you’re in the Terminal issuing commands, there is a file being updated in the background that you may not have been aware of. This file is called the Bash History file. This assumes, of course, that you’re running the Bash shell. It’s located in a hidden file underneath the $HOME directory of the current logged in user; that is to say, it’s located at ~/.bash_history. This file is generally referred to simply as the History file.

When you issue commands in the Terminal in Linux, these commands are recorded in this file. The default number of commands which are recorded may vary by Linux distro, but is typically somewhere in the range of 2000 – 5000. This number may be modified however. To modify the number of Linux command-line commands that get recorded in the History file, you will need to open a terminal editor like nano and edit a hidden file called ~/.bashrc.

modifying the history file in Linux

As shown above, I’m in the ~/.bashrc file and can see that my Bash History is set to 10000. This means that the Terminal will record 10000 commands I issue in Feren OS (based on Ubuntu and Debian) before it begins writing over previously-recorded commands in that file. But, why is it important to have such a file in the first place? Let’s take a closer look at this and also see how we can add a date/timestamp to the entries so that we know precisely when we issued any given command in the Terminal.

In the figure below, you can see the beginning of the history file which was generated when I issued the command:

$ history

The listing begins with the number 1 representing the first command issued since the History file was started or since the last time the History file was cleared. To clear the contents of this file, simply run the command:

$ history -c

history list

If, instead, I rerun the history command and look at the tail of this file, you can see that several commands have been recorded since my ~/.bash_history file was first generated. The output indicates that a total of 4836 commands have been recorded with the last command being the command which launched the output shown below:

eof of the History File

The importance of the History file is that by observing the contents of the file the Linux user can rerun any command previously run simply by noting the number to the left of the command shown in the list. For example, if I wish to rerun the command which generates a list of snap packages on my Feren OS system (corresponding to number 4824 on the History list), then all I need to do is to simply run the command:

$ !4824

executing History#4824

By issuing the ! (bang) symbol or Shift-1 on the standard keyboard, followed by the number of the command in History you wish to execute, the previously-recorded command is instantly executed and the output displaying the snap packages currently installed on my Linux system is shown above.

So, how can we add a date/timestamp to the list of commands recorded in the History file and why is this important? The importance to having a date/timestamp is that many commands are repeated in the Terminal and it would be a good idea to be able to tell just when such a command was issued and executed.

If you look once again at the list commands recorded in History and revealed by running the history command in the Terminal, you can clearly see that by default, the date/timestamp info is omitted. To add the date/timestamp to these commands, you need to edit the ~/.bashrc file once again and add the following entry somewhere in the vicinity of the HIST section:

HISTTIMEFORMAT="%Y-%m-%d %T "

Then save and close the file and reload your Terminal session by exiting and restarting a new Terminal session. After entering the history command once again, you’ll get a different display of output, which reveals the exact year, month, day and time that your command was entered:

last 10 commands issued in History

Note that by leaving an extra space following the %T before closing the double quotes in the HISTTIMEFORMAT command above, an extra space separates the date/timestamp and the actual bash command which was executed. Therefore, the history command that was issued to show the results in my Terminal above was issued at precisely 5:53:30 PM EDT on November 28, 2021. Since the ~/.bashrc command was modified to force the date/timestamp info in the History file, this action will be persistent and will survive a reboot or restart of the system since the ~/.bashrc file is read each time the user logs in.

Category