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.
Also Read: How to Set Environment Variables in Linux
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.
Table of Contents
Linux History File Location for All Popular Shell
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.
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.
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.
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
Suggested to read: How to remove duplicate lines in a text file
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.
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.
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.
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 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.
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.
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.

Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.