Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 2849

Docs, HowTos, Tips & Tricks • [HowTo] Managing bash history

$
0
0
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:


1. Increase the number of recalled commands


If you want your system to remember more commands, we will need to edit the bashrc-file. Open your terminal emulator (Konsole/XTerm, etc.)


nano ~/.bashrc

Look for the following lines, here with values 1000 and 2000.

Code:

export HISTSIZE=1000export HISTFILESIZE=2000

Let's increase these numbers to something larger:

Code:

export HISTSIZE=10000export HISTFILESIZE=20000
This 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.



2. View your history

You can get a list of your Bash history if you type history in the terminal

Code:

history

3. Editing, clearing and reloading the history file

The command history -a writes a new command to the history file

Code:

history -a
The command history -d [number] deletes an entry from the session's history. To make this permanent, you would need to also remove the command from .bash_history.

For example, to delete command number 25 in the session history list:

Code:

history -d 25
The command history -c clears session history

Code:

history -c
The command history -r reloads history



4. Searching for a command in history

Method 1:

We can use grep to find a particular command, for instance usage of the rm command:

Code:

grep rm  ~/.bash_history
This will show usage of the rm command from the command line interface.


Method 2:

In the command line interface, we can search for a command in history, we can use reverse search Ctrl+R. This is paricularly useful if you can't remember the command precisely.

Once you have the terminal emulator open, press the Ctrl and R key.

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.


That's it. I hope it was useful.

Statistics: Posted by Hallvor — 2024-11-01 12:47



Viewing all articles
Browse latest Browse all 2849

Trending Articles