Americas

  • United States
sandra_henrystocker
Unix Dweeb

Making use of your command history on Linux

How-To
Apr 22, 20256 mins

The bash history feature can save you a lot of time when repeating commands or examining commands you’ve entered previously.

Diverse Office: Indian IT Programmer Working on Desktop Computer. Female Specialist Creating Innovative Software. Engineer Developing App, Program, Video Game. Writing Code in Terminal.
Credit: Gorodenkoff / Shutterstock

The .history file in Linux – whether ~/.bash_history, ~/.zsh_history or ~/.history – provides ways to track and reuse commands that you have recently run. This post suggests how you might make good use of your command history and save yourself some time and trouble.

First, to simply view your command history, you can type “history” on the command line. Unless you are a brand new user on the system, the contents of this file will contain more than a screenful of commands. Note that the commands will be numbered and the list will display the more recently used commands last. Commands that you have run in your current session will by default not be included in your .bash_history file, but will be added once you close your terminal session.

$ history
1 cd
2 pwd
3 ls -a
4 ls -al
5 uname -a
6 hostname
7 df -k
8 cd /home
9 ls
10 du -sk *

You can also display the content of your history file with a command like “more ~/.bash_history”.

If you want to display a specific command that you’ve run recently, you can use grep to find and display it for you. Here’s an example:

$ history | grep report

You can rerun any command in your command history by typing the number shown on the left preceded by an exclamation point. For example, !83 would rerun the 83rd command in your command history. If you’re listing your command history, you can use a reverse search (i.e., go back up through the displayed commands) by pressing Ctrl-r and typing part of the command you want to rerun. This can be especially helpful if the command to be rerun is complex.

(reverse-i-search)`': more

Alternately, you can rerun a command by following an exclamation point by the first letters of the command you want to rerun (e.g., !grep). To rerun the command you just ran with a change, you can do something like this:

$ echo hello
hello
$ ^hello^goodbye
echo goodbye
goodbye

By default, commands that you enter are only added to the history file when you log out. You can, however, change this behavior by adding the command shown below to your .bashrc file.

export PROMPT_COMMAND='history -a'

Source your .bashrc file (. .bashrc) if you want this change to be made right away, else the change will take hold on your next login.

Want to see how many times you’ve run the commands recent command? Try this:

history | awk '{CMD[$2]++} END { for (a in CMD) print CMD[a] " " a }' | sort -rn | head

You should see a listing of your commands with the most often used listed first.

$ history | awk '{CMD[$2]++} END { for (a in CMD) print CMD[a] " " a }' | sort -rn | head
30 ls
11 cd
9 history
8 man
7 echo
6 which
6 pwd
6 more
6 cat
5 ps

Only a portion of your command history will be preserved. The number of commands that are saved will depend on your HISTSIZE setting. Use the command “echo $HISTSIZE” to see this setting. If you want to change it, add a line like this to the bottom of your .bashrc file:

HISTSIZE=3000

If you find that reusing previously run commands is something you commonly do, you might consider turning some of them into aliases. For example, you might add aliases like these to your .bashrc file.

alias c=”clear”
alias grep='grep --color=auto'

If you want to clear your current command history, use a command like this one:

$ history -c    

This will not affect the contents of your .bash_history file, but just your commands from your current session.

Adding the lines below to your .bashrc file will set the number of commands that will be saved in memory and in your .bash_history file as well as removing consecutively run duplicate commands and any commands that begin with spaces (making it easy to run commands that you don’t want saved in your history).

export HISTSIZE=1000
export HISTFILESIZE=2000
export HISTCONTROL=ignoredups:ignorespace

By default, the commands you have run in your current login session are only added to your .bash_history file at logout. To update after every command instead, add this line to your ~/.bashrc file:

export PROMPT_COMMAND='history -a'

Then, try this:

$ echo I want the world to be a safe and happy place for everyone
I want the world to be a safe and happy place for everyone
$ tail -2 .bash_history
export PROMPT_COMMAND='history -a'
echo I want the world to be a safe and happy place for everyone

There’s no rule against editing your history file, just enter a command like “vi .bash_history” and remove or alter any of the entries to suit yourself. Just remember that the older of the saved commands will be gone as you keep entering other commands.

If you want to add timestamps to commands that are saved in your command history, use a command like this:

$ export HISTTIMEFORMAT="%F %T "

To make this permanent, add the setting to your .bashrc file like this:

$ echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc

The commands below will clear your history buffer and overwrite your .bash_history file.

$ history -c; history -w
$ ls -l .bash_history
-rw-------. 1 jdoe jdoe 0 Apr 19 14:44 .bash_history

Wrap-up

The history file (.bash_history) on your Linux system can be useful if you want to review previously run commands or turn some of them into aliases to make them easier to run again and again. Controlling how many commands are saved and restricting which commands are saved may make reusing commands quite a bit more efficient.

sandra_henrystocker

Sandra Henry-Stocker was a programmer, Linux systems administrator, security engineer and Linux journalist for most of her 30-year career. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders. Tune into her 2-Minute Linux video tutorials and take command of your command line.

More from this author