Linux History Command: A Comprehensive Guide

This guide delves into the features and functions of the Linux history command, providing you with the skills to utilize it effectively.

The Linux history command is a powerful tool that allows you to access and manage your past terminal commands. The ability to search, edit, and execute previous commands can significantly boost your efficiency and productivity.

Whether you are a beginner or an experienced Linux user, this guide will provide valuable insights and tips for maximizing the use of the history command.

Show When the Command Is Executed

When you type history in the command line, you’ll typically see a number followed by the commands you’ve recently used.

The history command native behavior
The history command native behavior

However, displaying the timestamp alongside the command may be helpful for auditing purposes. To enable this functionality, you must configure the HISTTIMEFORMAT bash variable, as shown below.

export HISTTIMEFORMAT="%F %T "Code language: JavaScript (javascript)

Now, if you rerun the history command, the output will look like as follows:

Show when the command is executed
Show when the command is executed

Remove the variable to revert to the default mode of viewing.

unset HISTTIMEFORMATCode language: PHP (php)

Searching Command History

This may be your most often-used history command feature. After executing a lengthy command, you can search history for a keyword and re-execute it without typing it out again. Hereโ€™s how to use this built-in functionality.

  1. Press Ctrl+R and type the keyword.
  2. Keep pressing Ctrl+R until you reach the command you are looking for.
  3. Press “Enter” when you see it, which will execute the command.

I searched for “apt” in the following example, which displayed the first match found “sudo apt update” in the history containing the word “apt.”

Searching command history
Searching command history

However, you may want to edit a command from history before executing it. Press the โ€œEscโ€ key when you reach the match, make the necessary changes to the command, and then re-execute it by pressing โ€œEnter.โ€

Repeat the Last Executed Command

You may find yourself repeating the previous commands for various reasons. Following are the four different ways to repeat the last executed command.

  • Use the up arrow to view the previous command and press enter to execute it.
  • Type !! and press enter from the command line
  • Type !-1 and press enter from the command line.
  • Pressing Ctrl+P will display the previous command, then press “Enter” to execute it.

Execute a Specific Command from History

Sometimes we want to run a specific command from the history list. For example, let this be command #7, “sudo apt search chrome-gnome-shell.”

Execute a specific command
Execute a specific command

To do so, type “!” followed by the command number into the terminal. In our case, this would be “!7“.

Execute a specific command
Execute a specific command

Limit the Number of History Items

The HISTSIZE and HISTFILESIZE built-in Bash shell variables control this. So, add the following two lines to your “~/.bashrc file,” then log out and back in.

HISTSIZE=2000
HISTFILESIZE=2000

As a result, now history is set to store 2000 commands. By default, in most Linux distributions, the history size is limited to 500 or 1000 records.

Change the History File Name

By default, history is stored in the “~/.bash_history” file. To change the file that keeps your command history to, for example, “~/.my_commands,” add the following line to the “~/.bash_profile” file, then log out and log in again.

HISTFILE=~/.my_commands

The HISTFILE variable holds the name and location of your Bash history file. This is useful when tracking commands using different history file names from separate terminals.

Remove the Continuously Repeated Entries

If we frequently execute a command, its presence will be repeated many times. For example, the history command itself appears seven times below.

Remove the continuously repeated entries
Remove the continuously repeated entries

To remove all previous duplicate matches, set HISTCONTROL to erasedups, as shown below.

export HISTCONTROL=erasedupsCode language: JavaScript (javascript)

If we rerun the history command, the repeated matches will be gone except for the most recent occurrence.

Remove the continuously repeated entries
Remove the continuously repeated entries

I must make a significant distinction here. Suppose the HISTCONTROL variable is set to ignoredups instead of erasedups. In that case, the command will not be added to history if it has previously been executed and written to history, but this will not remove all of its previous appearances in history.

Hide a Particular Command from History

Now we’ll look at a specific feature of the history command. Namely, when you run a command, you can tell history to ignore it by setting HISTCONTROL to ignorespace and putting a space before the command, as seen below.

Note that there is a space at the beginning of “ping …“, which instructs the history command not to log it.

Hide a particular command from history
Hide a particular command from history

Of course, while using this feature, remember that purposefully hiding commands from history is a sure way to get in trouble during a subsequent system audit. Therefore, unless you have a sufficiently valid and legitimate reason to explain your actions, my advice is to refrain.

Clear All History

Sometimes you may want to wipe all previous command’s history. This is simple to accomplish with the command below.

history -c

Of course, as in the previous example, all caveats apply.

Remove a Specific Entry from the History

The history command also allows you to remove only a specific record. For example, assume this is line 4 of the following list.

Remove a specific entry from the history
Remove a specific entry from the history

To remove it, pass the “-d” parameter to the history command, followed by the record number you want to remove.

history -d 4

Next, if we check, we will see that it is already missing from the list of executed commands.

Remove a specific entry from the history

Ignore Specific Commands

You may not want to clutter your history with frequently used commands such as cd, pwd, and ls. In that case, HISTIGNORE lets you choose which commands should not be included in the history.

Use the “:” symbol for splits to list multiple commands, as shown below.

export HISTIGNORE="cd:pwd:ls"Code language: JavaScript (javascript)

Please note that adding ls to the HISTIGNORE ignores only ls but not, for example, ls -l. In other words, you must provide the exact command you want to remove from the history.

Disable the Usage of History

Set the HISTSIZE to 0 to disable history entirely and prevent the Bash shell from remembering the commands you’ve typed.

export HISTSIZE=0Code language: JavaScript (javascript)

Conclusion

The Linux history command is a powerful tool that allows users to view and manipulate the command history of their terminal sessions. It allows users to search for specific commands, repeat previous commands, and even edit and rerun commands from the history.

This can be especially useful for users who frequently use the command line, as it allows them to access and reuse previously entered commands easily. This guide introduces you to all the more significant aspects and applications of the history command to broaden your skill set.

For more about the history command in Linux, consult its manual page.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%