15 Powerful History Command Tricks to Supercharge Your Linux Shell
This guide walks you through 15 practical ways to use the Bash history command—show timestamps, search with Ctrl+R, repeat previous commands, customize history size, filter duplicates, and more—helping Linux users boost productivity and master command-line efficiency.
If you frequently use the Linux command line, mastering the history command can greatly improve your efficiency. Below are 15 useful usage examples.
Display timestamps with HISTTIMEFORMAT
Set the HISTTIMEFORMAT environment variable to show timestamps when you run history. The timestamps only apply to commands executed after the variable is set.
Search history with Ctrl+R
Press Ctrl+R to search through previous commands. Press Enter to execute the found command, or use the left/right arrow keys to edit it before execution.
Quickly repeat the last command
Four ways to repeat the previous command:
Press the up‑arrow key and hit Enter .
Type !! and press Enter .
Type !-1 and press Enter .
Press Ctrl+P and hit Enter .
Execute a specific command from history
To run the fourth command again, use !4:
Run a command by keyword
Enter !ps to execute the most recent command that starts with ps:
Control total history lines with HISTSIZE
Add the following two lines to .bash_profile and re‑login; the history will keep 450 entries:
Change the history file name with HISTFILE
By default history is stored in ~/.bashhistory. Adding the lines below to .bashprofile and re‑logging in will store history in .commandline_warrior instead:
Remove consecutive duplicate entries with HISTCONTROL
Set HISTCONTROL=ignoredups to skip consecutive duplicate commands.
Erase all duplicate entries with HISTCONTROL
Use HISTCONTROL=erasedups to remove all duplicate commands from the history.
Force history not to record specific commands
Set HISTCONTROL=ignorespace and start a command with a space to prevent it from being saved.
Clear all history with -c
Run history -c to erase the entire command history.
Command substitution
Use !!:$ to reuse the last argument of the previous command. The equivalent shortcut !$ works as well.
Similarly, !^ retrieves the first argument of the previous command:
Replace a specific argument for a command
Use !cp:2 to fetch the second argument of the most recent cp command:
Use !cp:$ to get the last argument of the cp command:
Disable history with HISTSIZE
Set HISTSIZE=0 to turn off command history recording.
Ignore specific commands with HISTIGNORE
Configure HISTIGNORE to skip commands like pwd, ls, and ls -ltr from being saved.
Original source: https://linuxtoy.org/archives/history-command-usage-examples.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
