Practical Tips for Using the Linux History Command to Manage Command History
This article explains how to efficiently use the Linux history command, covering its underlying mechanism, basic usage, repeat execution, searching, interactive search, timestamp display, history size control, file name changes, disabling history, and a useful security tip, helping administrators improve productivity and auditability.
When we frequently use the Linux command line, making effective use of the command history can greatly improve work efficiency. Repeating commands is common, and system administrators often need to audit user actions, making proper management of the Bash history essential.
1. Basic Principle
The command history is persisted in the user's home directory file .bash_history. When a shell starts, it reads this file into an in‑memory buffer. All commands you run are first stored in this buffer; the history command operates on the buffer, not directly on .bash_history. When the shell exits (e.g., by pressing Ctrl+D), the buffer is written back to .bash_history.
2. Detailed Usage
(1) Basic Usage
Running history displays all recent commands.
$ history
1 bash
2 ls
3 vim .bash_history
4 cat .bash_history
5 history
6 bashTo show only the last N entries, append the number:
$ history 10To force the buffer to be written to the file, use the -w option:
$ history -wTo clear the buffer (useful for sensitive commands), use -c:
$ history -c(2) Repeating Commands
Use ! to quickly repeat commands. For example, to repeat command number 1024:
$ !1024Repeat the previous command:
$ !!Repeat the command that is six entries from the end using a negative index:
$ !-6(3) Searching History
To repeat the most recent command that starts with a given string, use !string. For instance, to rerun the last curl command:
$ !curlAppend :p to preview the command without executing it:
$ !curl:p
curl www.sina.com.cnUse ? to search for any command containing a substring:
$ !?sina(4) Interactive Search
Press Ctrl+R to enter reverse incremental search. Type a keyword, press Ctrl+R repeatedly to cycle through matches, and hit Enter to execute the selected command.
(reverse-i-search)`sina': echo sina(5) Repeating the Last Command (Various Shortcuts)
!! !-1 Ctrl+p Uparrow Ctrl+R (interactive search)
(6) Displaying Timestamps
Set HISTTIMEFORMAT to show timestamps:
$ 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 more detailed audit information, include host and user data:
$ 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
The default buffer holds 1000 entries ( HISTSIZE). Increase it as needed:
$ echo $HISTSIZE
1000
$ export HISTSIZE=10000To control the file size, adjust HISTFILESIZE. Persist changes by adding them to ~/.bash_profile:
$ 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:
$ echo "export HISTSIZE=0" >> ~/.bash_profile
$ echo "export HISTFILESIZE=0" >> ~/.bash_profile
$ source ~/.bash_profile(10) A Little Hacker Trick
Prefix a command with a space to prevent it from being recorded, provided HISTCONTROL includes ignorespace.
3. Summary
In Linux, the history command offers powerful ways to manage command records. Commands are first stored in a buffer and written to .bash_history on shell exit. Proper configuration of related environment variables ( HISTSIZE, HISTFILESIZE, HISTIGNORE, HISTTIMEFORMAT, HISTCONTROL) makes the system more robust and secure.
Key usage patterns include: history n – show the last n entries history -c – clear the buffer history -w – write buffer to file history -d N – delete entry N
Repeating commands can be done with !!, !-1, !N, !string, etc. Interactive search uses Ctrl+R. Persisting environment variable settings in ~/.bash_profile ensures they survive reboots.
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.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.
