Linux History Command with Advance Examples

All the commands get stored by the shell interpreter: Find where it is stored, how to make it useful; and clear the history data if there’s something you don’t want to save in the record. 

Every time you execute a command in your terminal app (GNOME Terminal, Konsole, etc.), you will get the result without knowing that your interpreter, hiding behind the terminal, captures every executed command.

There are multiple famous Linux interpreters, such as bash, zsh, fish, etc., with the feature of capturing user executed commands into a specific file known as history.

  • bash stores all the user executed command records in the ~/.bash_history file.
  • zsh stores all the user executed command records in the ~/.zsh_history file.
  • fish stores all the user executed command records in the ~/.local/share/fish/fish_history file.

List All Executed Commands in Linux

Every time you hit the history command in your terminal, you will get the command captured by your shell interpreter (bash, zsh, fish) with the index number in order, as shown below.

$ history

Below is the output of the above command.

History Command in Linux
History Command in Linux

The above output might differ depending on the type of command you have executed on your system.

In front of all commands, you can see the index number, and you can use the index number to execute that specific command directly.

Execute the Commands from History using the Index Number

In history output, you get the command with their index number, which you can use to execute it directly using the "!" as shown below.

$ !3

Below is the output of the above command.

Execute the Commands from History using Index Number
Execute the Commands from History using Index Number

This feature might be helpful to re-execute previous large commands without retyping.

Display the History Output with Date and Time

The history output does not include the date and time information of command execution. To enable this, you can export the global variable HISTTIMEFORMAT with a legal code as shown below.

$ export HISTTIMEFORMAT='%F %T  '
$ history

Below is the output of the above command.

Display the History Output with Date and Time
Display the History Output with Date and Time

Ignore Duplicate Commands in History

Every command you execute is captured and displayed in the history output, duplicating those commands.

Export the below global variable to maintain a unique history output without displaying the repeated command in the future.

$ export HISTCONTROL=ignoredups

Find the Reserved History Size

The history reserved a specific size for storing the user-executed command. These history files get overwritten once they reach their threshold size.

Use the below command to check your current history size.

$ echo $HISTIZE

Below is the output of the above command.

Find the Reserved History Size
Find the Reserved History Size

Prevent History from Recording Any Executed Command

You can set the history size limit to 0 to prevent history from recording any executed command by you, as shown below.

$ export HISTSIZE=0

Prevent History from Storing Certain Strings

You can set the history to ignore specific commands executed by you, and these might be sensitive commands you don’t want to keep records of in the history output, such as passwd or FTP client connection.

Use the below command and place all the commands separated with a colon (:) to avoid storing them in history.

$ export HISTIGNORE="passwd:ftp: " 

As shown below, the above command does not store the passwd and FTP commands.

Prevent History from Storing Certain String
Prevent History from Storing Certain Strings

Search Commands in History

Your history output might contain a lot of commands. Filtering out and finding the specific command can be done by piping the grep command with the history, as shown below.

$ history | grep who

The above command returns the command containing “who” in it.

Search Commands in History
Search Commands in History

Display Results One at a Time

Over time, the history command output becomes too big. In this situation, to check the executed command, you have to scroll to the top.

Instead, pipe less command with history to display the result of the history command one at a time on screen and navigate using the up/down arrow keys as shown below.

$ history | less

Below is the output of the above command.

Display Result One at a Time
Display Results One at a Time

Display the Old Executed Command First

Generally, the history command list gets huge over time. You can pipe the tac command with the history if you wish to see the output for old execution commands first.

$ history | tac

Below is the output of the above command.

Display Old Executed Command First
Display the Old Executed Command First

Save Output to a New File

For your record, you can save the history command output to a new file and read it later using the below command.

$ history > file.txt

All the history command output will be stored in file.txt, as shown below.

Save Output to a New File
Save Output to a New File

Clear the History of Commands

Use the below command to clear all the stored commands in the history.

$ history -c

If you have more tricks for the history command, share them in the comment section.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments