The command history feature in Linux makes it easy to review and repeat commands you’ve previously executed. Integrated into the bash shell, this functionality streamlines the process of re-running commands without the need to retype them, and aids in examining the commands you’ve run recently, which can be invaluable for tracking changes made during your session.
When you type “which history” in your terminal, you will receive a message indicating that the shell cannot locate it. This happens because the which command searches for executable files, rather than built-in shell commands.
This article explores the workings of the history command and offers guidance on its effective use. (For additional reference, check out the Bash command cheat sheet.)
To begin with, it’s essential to understand that bash maintains a record of the commands you enter in a file known as .bash_history. A key point to note about this file is that after you execute a command, it may not immediately appear at the end of your .bash_history file.
Simultaneously, if you utilize the history command to review previously entered commands, you’ll find that the recent command is included in the output.
This is due to bash maintaining a command buffer and only appending entries to the history file upon log off or shell exit. When you log in next, the commands from your prior session will have been included.
You have the ability to adjust the quantity of commands preserved in your history file, which generally defaults to 1000. To verify the length of your command history, simply enter the command presented below.
If you wish to modify this limit to a different amount, you can append a line at the end of your .bashrc file with a command similar to those illustrated below, which will increase the number of saved commands to 2,000. The fourth line will source the .bashrc file to implement the change.
You have the ability to modify the size of your command history if you realize you’re either accumulating too many or not enough entries.
To adjust the default value for $HISTSIZE, you need to have root privileges and make changes within the /etc/profile file. Keep in mind that this adjustment will not affect existing accounts.
To prevent the same command from being saved multiple times when entered consecutively, you can utilize the HISTCONTROL option to disregard duplicates. To verify your current settings, execute the following commands:
The displayed settings will indicate the name and location of your history file, the quantity of commands that will be stored, as well as the fact that the ignoredups option is already active. If you run the whoami command three times in succession and then execute the command provided, you will find that the whoami command appears only once in the list.
If you want to execute a command that’s saved in your history, you can do so by using an exclamation point followed by the line number of the command. For instance:
You will see the command appear, followed by its execution.
Additionally, you can navigate through previous commands by pressing the up arrow key on your keyboard. This action will scroll you through your command history, allowing you to go from the most recent command to earlier ones. When you find the command you wish to execute, simply press the Enter key. Keep in mind, you have the option to edit the command before hitting Enter.
Another method to rerun a command is to use an exclamation point followed by the first few letters of the command you wish to execute again. This technique will retrieve the most recent command that corresponds to the letters you input. Here’s a straightforward example:
The command history is an invaluable resource for quickly executing previously used commands, particularly those that are intricate and time-consuming to retype. However, not every command you frequently use is significant enough to remember. Utilizing the command below will ensure that the four specified commands will *not* be recorded in your command history.
Once you apply a command like this, these particular commands will be excluded from your command history. For instance:
It’s important to note that commands defined in the HISTIGNORE configuration will not be stored in the history.
In this case, the date and pwd commands executed will be absent from the command history buffer once the HISTIGNORE configuration is activated. To keep these HISTIGNORE settings effective for future logins, be sure to add them to your .bashrc file.
Be aware that, according to the settings indicated previously, utilizing the history command will not be included in your command history file. Nevertheless, commands such as “history | tail -10” will still be recorded. To prevent this from happening, you can include “history *” in your settings as follows:
The Linux history command serves as a valuable and efficient tool. Managing the number of commands retained and limiting which commands are logged can significantly enhance the efficiency of reusing commands.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.