How To Use History Command In Linux.
In this article, we will talk about the history command, which allows you to view a list of previously executed commands, search through the list, and manipulate the history file.
Using the history Command and History Expansions
history is a shell builtin, and its behavior may slightly differ from shell to shell. We will cover the Bash builtin version of history.
In it’s simplest form, when invoked without any option or argument, the history command displays the whole history list with line numbers.
Typing !n executes the n-th command from the history list, and !-n the command n lines back. In the following example we’re executing the command on line 467:
Another way to execute a command is to use !word expansion. word refers to the most recent command starting with ‘word’.
Typically, history displays many lines of output that doesn’t fit on the screen. To view the output one page at a time, pipe it to a pager program like more or less command:
To display the last n lines, pass the number as an argument to the command. For example, to view only the last five lines from the history list you would type:
Use the up and down arrow keys to navigate the entries in the list. When the command you’ve searched for is shown press Enter to execute it.
Type !! to execute the previous command:
This is especially usefully when you forget to perpend a command with sudo , and instead of re-typing the command you can type:
!-1 is the same as !! and executes the last command from the history list, !-2 second to last, and so on.
^word1^word2^ expansion allows you re-run the last command replacing “word1” with “word2”. If you accidentally typed sduo command instead of sudo command you can repeat the command using the correct word with:
Use the grep command to filter the output. For example, to view all commands including “nano” you would run:
Now, if you want to re-run the nano setup.py command simply type:
Another way to search through the command history is by pressing Ctrl-R. The prompt will change to the following, and you can start searching for a previously executed command.
The shell will display a matching line. To move to the next matched suggestion, press Ctrl-R again.
Check the Bash manual for more information about History Expansion , modifiers, and designators.
Saving the History List
By default, when starting a new session, Bash reads the history list from the .bash_history file . The list of commands that are executed in the current session are kept in the memory and saved to the file when the session is closed.
If you opened several shell sessions, only the history of the session that is closed last is saved.
The -a option allows you to save the current session history list to the .bash_history file:
The -w option writes the complete history list to the history file.
The history command allows you to clear the complete history list or remove certain parts.
To clear the history list, use the -c option:
To delete a specific line or lines between a start and end positions from the history list, use the -d option.
For example, to remove the lines between 365 and 375 (including those lines), you would type:
If you provide only one number to the -d option, the command removes the given line.
When a negative integer is used, the lines are count back from the end of the history list.
The commands above clear the history list, which is kept in the memory, but does not remove entries from the .bash_history file on the disk. To clear the file, you need to write the history list to the file:
Modifying History Behavior
The behavior of the Bash history can be defined using several different environment variables . When modifying the history behavior, set the variables in ~/.bashrc or any other configuration file which is loaded when the user logs in.
By default Bash keeps 500 lines in the command history list. The HISTSIZE variable allows you to change this value. To set it to 10000 add the following line to your .bashrc file:
The HISTCONTROL variable accepts a colon-separated list of values that define how the commands are saved in the history list:
ignorespace – commands that start with space are not saved in the history list.
ignoredups – duplicate commands are not saved.
ignoreboth – is a shorthand, including both ignorespace and ignoredups.
When the HISTTIMEFORMAT variable is set, Bash prepends a timestamp of execution for the command on each line.
For example, if you set:
The history will be displayed in the following format:
The history command displays a list of previously executed commands. For example, you can use the history to view a long command you’ve used before that you can’t remember.