Master Linux Command History: 10 Powerful Tips to Boost Efficiency
This article explains how Linux shells store command history in .bash_history, shows basic and advanced usage of the history command—including listing, searching, repeating, timestamping, and configuring history size—and provides practical tricks for auditing, backup, and even disabling history for security purposes.
Hello, I am Chopin and this is my 15th original article.
When we frequently use the Linux command line, effectively using the history can greatly improve work efficiency. In everyday Linux operations many commands are repeated, and system administrators often need to audit user actions, making proper management of the command history essential.
1. Basic Principle
Linux command history is persisted in the user's home directory in the .bash_history file. When a shell starts, it reads this file into an in‑memory buffer. Commands you run are stored in that buffer; the history command operates on the buffer, not directly on the file. When the shell exits (e.g., pressing Ctrl+D), the buffer is written back to .bash_history.
2. Detailed Usage
Basic Usage
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 lines with history N, for example: $ history 10 Force write the buffer to the file with history -w and clear the buffer with history -c.
$ history -w
$ history -cRepeating Commands
Use ! to quickly repeat a command. For example, repeat command number 1024: $ !1024 Repeat the previous command: $ !! Repeat the command that is N entries back, e.g., the 6th last command:
$ !-6Searching History
Execute the most recent command that starts with a given string using !string. To preview the command without executing, append :p:
$ !curl:p
curl www.sina.com.cnSearch for any command containing a substring with ? (e.g., ?x).
Interactive Search
Press Ctrl+R to enter reverse‑i‑search mode, type a keyword, and press Ctrl+R repeatedly to cycle through matches.
(reverse-i-search)`sina': echo sinaDisplaying Timestamps
Add timestamps to history entries for auditing:
$ 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 info, include user and host:
$ 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 historyControlling History Size
Check the current buffer size with echo $HISTSIZE (default 1000). Increase it as needed: $ export HISTSIZE=10000 To control the maximum number of lines stored in .bash_history, set HISTFILESIZE.
Persist these settings by adding them to ~/.bash_profile and reloading the profile.
$ echo "export HISTSIZE=10000" >> ~/.bash_profile
$ echo "export HISTFILESIZE=200000" >> ~/.bash_profile
$ source ~/.bash_profileChanging History File Name
Redirect history to a custom file with HISTFILE:
$ echo "export HISTFILE=/data/backup/chopin.bash_history" >> ~/.bash_profile
$ source ~/.bash_profileDisabling History
Set both HISTSIZE and HISTFILESIZE to 0:
$ echo "export HISTSIZE=0" >> ~/.bash_profile
$ echo "export HISTFILESIZE=0" >> ~/.bash_profile
$ source ~/.bash_profileLittle Hack for Security
Prefix a command with a space to prevent it from being recorded, provided the HISTCONTROL variable includes ignorespace.
3. Summary
The history command in Linux is a powerful tool for managing previously executed commands. By configuring environment variables such as HISTSIZE, HISTFILESIZE, HISTTIMEFORMAT, and HISTCONTROL, administrators can tailor history behavior for auditing, backup, or security needs, making the system more robust and safer. history n: show the most recent n entries history -c: clear the buffer history -w: write buffer to file history -d N: delete entry N HISTSIZE: max entries in buffer HISTFILESIZE: max entries in file HISTIGNORE: patterns to ignore HISTTIMEFORMAT: timestamp format HISTCONTROL: extended control options
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.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.
