Capitalizing on Your Linux Command History

When working with Linux, I often forget commands I don’t use regularly — and I definitely don’t enjoy retyping long ones. Luckily, bash keeps a history of previously executed commands, which we can leverage to save time and boost productivity.

Making the Most of Bash History

The simplest way to access past commands is by pressing the up and down arrow keys. A more powerful method is using Ctrl-R, which lets you start typing a command and autocompletes it based on history.

This command history is surprisingly valuable. Once a complex command is typed correctly and saved, it becomes a reference you can reuse forever. Over time, this small habit compounds into serious productivity gains.

But we don’t have to settle for the default history behavior — we can enhance it significantly by increasing history retention and using better tools to search through it.

Improving Bash History Retention

Start by tweaking your ~/.bashrc file. My setup is inspired by this repository, and here’s what I use:

## SANE HISTORY DEFAULTS ##
shopt -s histappend              # Append to history file, don't overwrite
shopt -s cmdhist                 # Save multi-line commands as one
PROMPT_COMMAND='history -a'     # Save history after each command
HISTSIZE=500000                 # Keep a huge in-memory history
HISTFILESIZE=10000000           # ...and a huge file-backed one
HISTCONTROL="erasedups:ignoreboth" # Avoid duplicates
export HISTIGNORE="&:[ ]*:exit:ls:bg:fg:history:clear" # Skip noisy commands
HISTTIMEFORMAT='%F %T '         # ISO 8601 timestamps

I also version control my .bash_history file. That way, when I work on a new machine, I can pull in my previous history and append it to the local one. This has allowed me to retain several years of command history!

Security Considerations

While storing long histories is great for productivity, be aware that anything typed into your shell could be saved, including sensitive information like passwords, API keys, or secret tokens.

A few tips:

  • Avoid putting secrets in commands (use environment variables, config files, or prompts instead).

  • Consider using HISTIGNORE to skip known risky patterns.

  • Encrypt or restrict access to your .bash_history file, especially if you're version-controlling it.

  • Review and clean history periodically.

Using a Better Tool: hstr

While Ctrl-R is good, there are more powerful tools for history search like fzf, mcfly, and my personal favorite — hstr.

hstr enhances your history navigation with an intuitive, interactive interface. It supports ranked commands, favorites, and keyword-based search — all from the terminal.

Here’s my configuration (added to ~/.bashrc):

# HSTR configuration
alias hh=hstr
export HSTR_CONFIG=hicolor,raw-history-view,blacklist,substring-matching

# Sync history between memory and file
export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"

# Bind hstr to Ctrl-R (for interactive shells)
if [[ $- =~ .*i.* ]]; then bind '"\C-r": "\C-a hstr -- \C-j"'; fi

# Optional: bind Ctrl-X K to kill the last command
if [[ $- =~ .*i.* ]]; then bind '"\C-xk": "\C-a hstr -k \C-j"'; fi

(Note: I don’t use the tool’s default config because it overrides HISTSIZE and HISTFILESIZE.)

Features I Love:

  • View commands according to

    • History

    • Ranking (most used)

    • Favorites (I don’t use that but you can pin your favorite commands)

  • Search commands by

    • Match exact (commands starting with the search string)

    • Match Regexp

    • Match keywords

  • Configure case sensitivity for the search

Real-World Example

A while back, I was testing a Docker-in-Docker (dind) setup inside a Kubernetes cluster. I needed to run a kubectl command that launched a temporary pod with a privileged security context — all done in a one-liner using JSON overrides. Definitely not something I wanted to memorize.

To find this command back, I type Ctrl-R to open the hstr tool. Then I type two times Ctrl-E to search by keywords. I remember that it was a kubectl command and that I used privileged somewhere in that command, so I just need to write those two keywords.

Boom — hstr pulled it up instantly:

kubectl run -n edp tmp-shell --image=docker:18.09.6-dind --rm -i --tty --overrides='{"spec": {"containers": [{"name": "tmp-shell", "image": "docker:18.09.6-dind", "securityContext": {"privileged": true} }]}}'

Nice and easy. 😎

Addendum: hstr Compatibility Note

As of Linux kernel 6.2.0, hstr stops functioning properly if CONFIG_LEGACY_TIOCSTI is not set. The interface still opens and browses history, but it can no longer insert commands back into the shell.

This issue is tracked here: dvorka/hstr#478

Due to this limitation, I’ve since switched to fzf for my command history search. It’s fast, flexible, and plays nicely with newer kernel versions.

If you often work in the terminal, enhancing your history usage can save you countless hours and mental cycles. Don’t underestimate the power of remembering… without needing to remember.

0
Subscribe to my newsletter

Read articles from Thibaut Tauveron directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Thibaut Tauveron
Thibaut Tauveron

👋 Hi, I’m a cloud engineer and cybersecurity enthusiast based in Zürich. I’ve worn many hats over the years—developer, DevOps consultant, SRE, cloud architect—and what ties it all together is a passion for building secure, scalable systems that just work. I write about cloud infrastructure, DevSecOps, and anything that helps teams move faster without breaking things. I believe in automation, simplicity, and sharing knowledge—whether through blog posts, open source, or mentoring.