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! So what is this command, and what are its uses? First, let’s discuss this in more detail.

Linux Commands for Beginners 15 - Bash History

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

For example, 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. Instead, 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 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 to find 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 you used it.

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! Of 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

Wrapping up

We have learned the basics 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?

HakTip - Bash History Tricks

You may also be interested in

About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

Your email address will not be published.