This guide delves into the features and functions of the Linux history command, providing you with the knowledge and skills to utilize it effectively in your daily workflow.
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
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.
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 "
Now, if you rerun the
history command, the output will look like as follows:
Remove the variable to revert to the default mode of viewing.
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.
Ctrl+Rand type the keyword.
- Keep pressing
Ctrl+Runtil you reach the command you are looking for.
- 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.”
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.
!!and press enter from the command line
!-1and press enter from the command line.
Ctrl+Pwill 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.”
To do so, type “
!” followed by the command number into the terminal. In our case, this would be “
Limit the Number of History Items
HISTFILESIZE built-in Bash shell variables control this. So, add the following two lines to your “~/.bashrc file,” then log out and back in.
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 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.
To remove all previous duplicate matches, set
erasedups, as shown below.
If we rerun the history command, the repeated matches will be gone except for the most recent occurrence.
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
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.
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.
Of course, as in the previous example, all caveats apply.
Remove a Specific Entry from the History
history command also allows you to remove only a specific record. For example, assume this is line 4 of the following list.
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.
Ignore Specific Commands
You may not want to clutter your history with frequently used commands such as
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.
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
HISTSIZE to 0 to disable history entirely and prevent the Bash shell from remembering the commands you’ve typed.
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.