Master Linux Command History: Reduce Repetition and Boost Efficiency
This guide explains how the Linux shell stores command history, how to view, search, repeat, and manage entries, and how to configure environment variables for timestamps, size limits, custom files, and security, helping administrators streamline their workflow.
1. Basic Principle
The shell stores command history in the .bash_history file in the user's home directory. When a shell starts, it loads this file into an in‑memory buffer; commands are recorded in the buffer, not directly in the file. Upon exiting the shell (e.g., pressing Ctrl+D), the buffer is written back to .bash_history.
2. Detailed Usage
(1) Basic Commands
Run history to list recent commands.
$ history
1 bash
2 ls
3 vim .bash_history
4 cat .bash_history
5 history
6 bashShow only the last N entries with history N (e.g., history 10). $ history 10 Force a write of the buffer to the file with history -w. $ history -w Clear the buffer with history -c.
$ history -c(2) Repeating Commands
Use ! followed by a number to execute a specific entry, e.g., !1024 runs command #1024. $ !1024 Repeat the previous command with !! or the N‑th previous command with a negative index, e.g., !-6 runs the sixth‑last entry.
$ !!
$ !-6(3) Searching History
Execute the most recent command that starts with a string using !string, e.g., !curl. Append :p to preview without executing.
$ !curl:p
curl www.sina.com.cnSearch for any command containing a substring with !?substring, e.g., !?sina.
$ !?sina(4) Interactive Search
Press Ctrl+R and type a keyword to perform a reverse incremental search. Press Ctrl+R repeatedly to cycle matches, then Enter to execute.
(reverse-i-search)`sina': echo sina(5) Repeating the Last Command
!! !-1 Ctrl+p Uparrow Ctrl+R (interactive)
(6) Displaying Timestamps
Set HISTTIMEFORMAT to include date and time.
$ export HISTTIMEFORMAT='%F %T '
$ history 3
46 2021-04-18 15:21:33 curl baidu.com
47 2021-04-18 15:21:35 pwd
48 2021-04-18 15:21:39 history 3For richer info, embed host and user data in the format.
$ export HISTTIMEFORMAT="%F %T `who -u am i 2>/dev/null|awk '{print $NF}'|sed -e 's/[()]//g'` `whoami` "
6 2021-04-18 16:07:48 113.200.44.237 root ls
7 2021-04-18 16:07:59 113.200.44.237 root pwd
8 2021-04-18 16:08:14 113.200.44.237 root history(7) Controlling History Size
Check the current buffer limit with echo $HISTSIZE (default 1000). Increase it as needed: $ export HISTSIZE=10000 Control the file size with HISTFILESIZE. To make changes permanent, append them to ~/.bash_profile and source the file.
$ echo "export HISTSIZE=10000" >> ~/.bash_profile
$ echo "export HISTFILESIZE=200000" >> ~/.bash_profile
$ source ~/.bash_profile(8) Changing the History File Name
Set HISTFILE to a custom path.
$ echo "export HISTFILE=/data/backup/chopin.bash_history" >> ~/.bash_profile
$ source ~/.bash_profile(9) Disabling History
Set both HISTSIZE and HISTFILESIZE to 0 to turn off history recording.
$ echo "export HISTSIZE=0" >> ~/.bash_profile
$ echo "export HISTFILESIZE=0" >> ~/.bash_profile
$ source ~/.bash_profile(10) A Handy Hack
Prefix a command with a space to prevent it from being saved, provided HISTCONTROL includes ignorespace. Verify with echo $HISTCONTROL.
3. Summary
The history command offers powerful ways to view, repeat, search, and manage shell commands. Proper configuration of environment variables such as HISTSIZE, HISTFILESIZE, HISTIGNORE, HISTTIMEFORMAT, and HISTCONTROL enhances security and auditability. Persist these settings in ~/.bash_profile for production environments. history n: show last n entries history -c: clear buffer history -w: write buffer to file history -d N: delete entry N
Common repeat shortcuts: !!, !-1, !N, !string. Use Ctrl+R for interactive search. Adjust HISTSIZE, HISTFILESIZE, HISTTIMEFORMAT, and HISTCONTROL to suit your security and auditing needs.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
