Have you ever looked for a command in Bash history when using arrow up and arrow down and not found it? Here are a few Bash history tips and tricks to help you manage your command history more effectively.
If you want your system to remember more commands, you will need to edit the .bashrc
file. Open your terminal emulator (Konsole, XTerm, etc.) and use a text editor like nano
:
nano ~/.bashrc
Look for the following lines, which typically have values of 1000 and 2000:
export HISTSIZE=1000
export HISTFILESIZE=2000
Let’s increase these numbers to something larger:
export HISTSIZE=10000
export HISTFILESIZE=20000
This configuration stores up to 10,000 commands in memory per session and saves up to 20,000 commands in the .bash_history
file. Save and exit with Ctrl+X
.
If you run into issues, please reduce the number.
You can get a list of your Bash history by typing history
in the terminal:
history
Write new commands to the history file:
history -a
Delete an entry from the session’s history:
history -d [number]
For example, to delete command number 25 in the session history list:
history -d 25
To make this permanent, you would need to also remove the command from .bash_history
.
Clear session history:
history -c
Reload history from the history file:
history -r
grep
You can use grep
to find a particular command, for instance, usage of the rm
command:
grep rm ~/.bash_history
This will show usage of the rm
command from the command line interface.
In the command line interface, you can search for a command in history using reverse search (Ctrl+R
). This is particularly useful if you can’t remember the command precisely.
Once you have the terminal emulator open, press the Ctrl
and R
keys. Then start typing the start of the command, and earlier matches should show. Press Enter
to execute the command or press the right arrow key to exit search without running a command.
If you want to run a command without saving it in the history, you can prepend the command with a space. This requires the HISTCONTROL
variable to be set to ignorespace
or ignoreboth
in your .bashrc
file:
export HISTCONTROL=ignoreboth
Now, any command you run with a leading space will not be saved in the history.