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.

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
/home/ubuntu/.bash_history

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
1000

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 bbc.co.uk
    3  nano testDns.sh
    4  chmod 700 testDns.sh
    5  ./testDns.sh
    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 bbc.co.uk
    3  nano testDns.sh
    4  chmod 700 testDns.sh
    5  ./testDns.sh
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 bbc.co.uk
    4  dig @3.104.255.208 bbc.co.uk
    5  dig @3.104.255.208 anto.online
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 ./testDns.sh
     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?

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.