How to use the Bash History command

Do you have a hard time finding a previously used Bash command? If so, then you are in the right place.

Finding that command is straightforward when you use the Bash history! What is this command, and what are its uses? Let’s discuss this in more detail.

What is the Bash History Command?

The Bash History command stores all typed and executed commands. It is a GNU command and is very popular. Any user that runs a command will have their command saved in the .bash_history file.

The ~/.bash_history file is usually located in /home/user/.bash_history. You can run the ‘echo $HISTFILE’ command to find the location of your bash_history file.

user@server:~$ echo $HISTFILE

How much does the ~/.bash_history store?

The file has a limit, and can (by default) store up to 500 commands. The old commands start to get overwritten if the commands exceed this number. For example, if you have a limit of 100 commands, and you have filled that limit. Next, all the new commands will overwrite old commands starting from the first.

You can also check the limit of storage for your bash history file using ‘echo $HISTSIZE’:

user@server:~$ echo $HISTSIZE

You also have the power to set your preferred limit for storage for the current session:

export HISTSIZE=num

Changing “num” to 2000 will limit the HISTSIZE to 2000.

HISTFILESIZE is another attribute of the bash history file. This attribute indicates how many commands can be stored in the history file for all sessions. On the other hand, the HISTSIZE attribute indicates how many commands can be stored in the current session’s history file. You can also alter the value of HISTFILESIZE.

The commands executed during the current session are not added to the history file. These commands are stored in a history buffer. When the user logs out, then all the history buffer contents are written into the history file.

Using the history command:

Bash history makes it easy to search and read recorded commands. You have the power to view all commands at once or just view the last few. Also, you can view commands containing a specific keyword. Let’s look at how you can search through commands to find a particular command.

history command:

Executing this command will read the bash history file and output all stored commands in a numbered list in ascending order of execution.

user@server:~$ history
    1  ping merlin
    2  dig
    3  nano
    4  chmod 700
    5  ./
    6  mkdir nginx
history num command:

Sometimes you only need to view the latest commands. In this case, you can specify the number of last executed commands you want to see. Here the terminal will output five last executed commands.

user@server:~$ history 5
    1  ping merlin
    2  dig
    3  nano
    4  chmod 700
    5  ./
history | grep xyz command:

It can become difficult finding the exact command by scrolling through a list. In this case, you can search for all the commands that contain a specific keyword. In the example below, the terminal will output all commands that contain the keyword “dig.”

user@server:~$ history | grep dig
    2  dig
    3  dig
    4  dig @
    5  dig @
history | awk | sort | uniq | sort | head command:

Executing the commands below will output your top 10 most used commands. The output also displays a number beside the command that indicates how many times it was used.

user@server:~$ history | awk '{print $2}' | sort | uniq -c | sort -nr | head -10
     85 ls
     41 cd
     40 nano
     32 ssh
     32 ./
     27 sudo
     20 dig
     12 aws
     11 youtube-dl
     11 echo

How to clear your bash history

Clear your current history buffer with the following command:

history -c

The ‘history -c’ command will delete all commands for the current session. Keep in mind that this command will not clear your entire bash history. It clears only the current session’s bash history.

How to prevent storing credentials

All the commands are recorded in the history file, including your username, password, and other sensitive data. As you may suspect, this can prove to be dangerous! Off course, you don’t want the wrong persons reading sensitive data, like passwords, from the bash history.

You can disable the bash history from storing newly entered commands during your session:

set +o history

Also, you can enable storing commands by using the following command:

set -o history

In Summary

We have learned the basic of using the Bash History command. You even have a handy script to determine your most-used commands. Also, see our Most Used Linux Commands post for other great tips!

Why look at the following video to see some great Bash history tips from Hak 5?

Leave a Reply

Your email address will not be published. Required fields are marked *