If you experience any difficulty in accessing content on our website, please contact us at 1-866-333-8917 or email us at support@chicagovps.net and we will make every effort to assist you.

By
October 1, 2024

Enhance Your Productivity: Mastering Command History in Linux

 

The command history feature in Linux enables users to effortlessly review and repeat prior commands. This functionality is integrated into the bash shell, which is part of the shell executable. It’s particularly useful for rerunning commands without the need to type them again, or for looking back at recently executed commands to identify when or how a recent modification occurred.

When you enter “which history” in the command line, you will likely receive a message indicating that the shell cannot locate it. This is because the which command is designed to search for executables to run, not for built-in shell commands.

This article explores the mechanics of the history command and offers insights on how to utilize it effectively. (For more information, see: Bash command cheat sheet)

To begin, it’s important to note that bash maintains a record of the commands you input in a history file known as .bash_history. However, one crucial aspect to understand about this file is that after executing a command, you may find it is not immediately at the end of your .bash_history file – at least not right away:

Simultaneously, when you utilize the history command to view commands that have been recently entered, the command you just executed will appear in the results.

This behavior occurs because bash keeps a record of commands in a buffer and only integrates them into the history file upon logging out or closing the shell. Consequently, when you log in again, the commands from your previous session will be included.

You have the ability to manage the volume of commands saved in your history file. The default setting is typically 1000. To verify the length of your command history, enter the command displayed below.

If you wish to adjust this to a different number, you can append a line to the end of your .bashrc file with a command similar to those illustrated below, which will set the command saving limit to 2,000. The fourth line reloads the .bashrc file to implement your changes.

You have the ability to adjust the size of your command history if you discover that you’re either saving too many commands or not enough.

To modify the default setting for $HISTSIZE, you need to have root privileges and make the change in the /etc/profile file. Keep in mind, though, that this alteration won’t apply to existing user accounts.

In order to prevent saving the same command multiple times when it has been entered consecutively, you can use the HISTCONTROL option to ignore duplicates. To verify your current configurations, you can execute the following commands:

The output from these commands will indicate the name and path of the history file, the number of commands to be preserved, and confirm that the ignoredups setting is configured correctly. Thus, if you enter the whoami command three times successively and then execute the corresponding command, you will observe that whoami appears in your history only once.

If you want to repeat a specific command saved in your history file, you can do so by entering an exclamation point followed by the corresponding line number. For example:

The command will be shown and executed immediately.

You can easily navigate through your previous commands by pressing the up arrow key on your keyboard. This allows you to leap from the most recent command to earlier ones. Once you find the command you wish to execute, just hit the Enter key. Keep in mind, you can modify the command before pressing Enter.

Another method to repeat a command is to type an exclamation point followed by the initial letter(s) of the command you wish to execute again. This will retrieve the most recent command that begins with those letters. Here’s a straightforward example:

The command history is a valuable asset for re-executing commands, particularly when dealing with intricate operations, saving you the effort of having to input them repeatedly. However, not every command warrants retention in memory, especially those you frequently use. The command illustrated below will ensure that the specified four commands will *not* be kept in your history.

Upon executing such a command, those specified will be omitted from your command history. For instance:

It’s important to note that the commands listed in the HISTIGNORE configuration will not be stored in history.

The date and pwd commands were executed but skipped from being recorded in the history buffer immediately after applying the HISTIGNORE setting. To make this change persistent across logins, consider adding the HISTIGNORE configurations to your .bashrc file.

Keep in mind that, based on the settings displayed above, any usage of the history command will not be recorded in your command history file. Nonetheless, commands such as “history | tail -10” will still be stored. If you prefer to prevent this, you can include “history *” in your settings as shown below:

The history command in Linux can serve as an extremely handy and time-saving resource. By managing the number of commands saved and limiting which commands are logged, you can enhance the efficiency of reusing commands significantly.


ChicagoVPS is your gateway to unparalleled hosting solutions. Our state-of-the-art datacenters and powerful network ensures lightning-fast speeds and uninterrupted connectivity for your websites and applications. Whether you’re a startup looking for scalable resources or an enterprise in need of enterprise-grade hosting, our range of plans and customizable solutions guarantee a perfect fit. Trust in ChicagoVPS to deliver excellence, combining unmatched reliability and top-tier support.

For Inquiries or to receive a personalized quote, please reach out to us through our contact form here or email us at sales@chicagovps.net.

Subscribe Email

Top