Using Linux Command Line History
In this post we will discuss about the very powerful, and essential tool in linux. All commands that we type into the console in linux is never lost for ever, but its recorded and stored in such a way, that it is helpful in many number of ways for the user.
The normal default settings for the command line history can be customised to a large extent to suite different needs.
Now there are two files that we need to edit for editing some parameters for command line history.
.bashrc (located in the homedirectory of a user)
.bash_history(where history commands are saved.)
Change the history size:
Now inorder to change the history size, we need to add/modify the value of HISTSIZE variable in .bashrc file.
like in most default machines its 500. You can increase this to whatever you want.
a typical entry in .bashrc file is shown below.
Print the entire history file.
Now if you want to simply print all current history on the screen. Just use the command history without any options and arguments.
in the output, the left side column will shown the command number and right side the command itself
Note: the history file is always smaller than the total history, because commands from the current shell are only added to the history file after the shell is exited.
Run nth command in history
if you want to run suppose imagine the command which is on 500 history line(from the output of history command). You can do this by just following as below.
!<command number> for example !500
HISTIGNORE environment variable
the histignore variable can be used to specify which command can be ignored without saving in history.
suppose you want that the command that begin with fdisk should not be saved to history then you can do the following.
suppose imagine that you dont want history to save and remember your commands, then you can easily disable them by doing the following.
you can achieve this by setting histsize=0, and exporting it.
Prevent a command from getting included in history
Suppose you want to run a command, and you dont want that command to be part of the history then you can do that by creating a variable called HISTCONTROL
the above line tells that any command typed with a space before that will not be saved in hostory.
so type space first and then command to exclude it from history.
Disable duplicate commands from being entered to history.
erasedups value can be assigned to HISTCONTROL for not including all duplicate commands in history.
after the above command the history will not save duplicate commands, or say one command twice.