Server Administration & Management

How to save bash_history

I spend most of my time working in front of a black and white terminal of remote SSH connections to various
servers and bash history is a very important feature of bash that saves me much time by recalling previous
commands I have typed. History is a linux shell command that list all the executed command on server. Default
1000 commands can be displayed in history.Here are some tricks on how you can optimize with some simple
configurations settings the usage your bash history.

1. Don’t save duplicates:


this causes any lines matching the previous history entry not to be saved. Other options for

HISTCONTROL: ignorespace, lines which begin with a space character are not saved in the history list;
erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved.

2. Size of the history:


HISTSIZE: The number of commands to remember in the command history. The default value is 500.
You can set this to 0 and disable the usage of the history file.

3. Others:

HISTFILE: The name of the file in which command history is saved. The default value is ~/.bash_history

HISTIGNORE: A colon-separated list of patterns used to decide which command lines should be saved on the history list.

HISTTIMEFORMAT: Adding timestamp it will provide you with an additional information of date and time of execution of command. Which will help you inistigate as at what exact date and time command was executed.

You can set these options by Either export them in your environment in your personal bash configuration file (~/.bashrc) or in the global bash configuration file (/etc/bash.bashrc). The name of the configuration files can depend from your Linux distribution and bash version (the ones included are from Debian Linux), but you can always see your particular options using man bash. So, you can add in your configuration files the parameters you want like this:

shopt -s histappend: It will prefer to have just one history file, with all commands you typed in any ever opened terminal window,

# vi /root/.bash_profile “OR” vi /home/username/.bash_profile “OR” vi /tc/bash.bashrc

export HISTCONTROL=ignoreboth
export HISTCONTROL=ignoredups
export HISTCONTROL=ignorespace
export HISTSIZE=2000
export HISTFILESIZE=2000
export HISTFILE=.bash_alternate_history
shopt -s histappend

You will need to restart your bash session in order to activate the settings. You can check if your configuration were entered correctly by typing env at the command prompt. If you don’t see your configuration in the environment variables than you have done something wrong. If you see your configuration option, then all is ok, and your setting is active already.

NOTE :- “the most efficient way to search your history is to hit Ctrl R and type the start of the command. It will auto complete as soon as there’s a match to a history entry, then you just hit enter”

September 6, 2011 - Posted by | Tips & Tricks, Unix/Linux | ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: