Fundamentals 10 min read

Master Linux History Command: Practical Tips to Boost Shell Efficiency

This guide explains how the Linux history mechanism works, shows essential commands for viewing, filtering, and replaying past commands, demonstrates timestamping, adjusting record limits, changing the history file, disabling logging, and shares a hidden trick for keeping sensitive commands out of the log.

dbaplus Community
dbaplus Community
dbaplus Community
Master Linux History Command: Practical Tips to Boost Shell Efficiency

1. Basic Principle

The Bash history is stored persistently 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 Usage

Display the entire command history: $ history Show only the last N entries (e.g., 10): $ history 10 Force the current buffer to be written to the file: $ history -w Clear the buffer without affecting the file:

$ history -c

2) Repeating Commands

Execute a specific numbered command, e.g., entry 1024: $ !1024 Repeat the previous command: $ !! Repeat the command n lines back (negative index), e.g., the 6th last command:

$ !-6

3) Searching History

Run the most recent command that starts with a given string, e.g., curl : $ !curl Preview the command before execution using :p : $ !curl:p Search for any command containing a substring, e.g., sina :

$ !?sina

4) Interactive Search

Press Ctrl+R to enter reverse‑i‑search mode, type a keyword, and press Ctrl+R repeatedly to cycle through matches. Hit Enter to execute the displayed command.

(reverse-i-search)`sina': echo sina

5) Multiple Ways to Repeat the Last Command

!!
!-1
Ctrl+p
Up

arrow Ctrl+R (reverse search)

6) Display Timestamps

Set HISTTIMEFORMAT to show timestamps:

$ export HISTTIMEFORMAT='%F %T '</code>
<code>$ history 3</code>
<code> 46  2021-04-18 15:21:33 curl baidu.com</code>
<code> 47  2021-04-18 15:21:35 pwd</code>
<code> 48  2021-04-18 15:21:39 history 3

For richer audit info, include host and user:

$ export HISTTIMEFORMAT="%F %T `who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` `whoami` "</code>
<code> 6  2021-04-18 16:07:48 113.200.44.237 root ls</code>
<code> 7  2021-04-18 16:07:59 113.200.44.237 root pwd</code>
<code> 8  2021-04-18 16:08:14 113.200.44.237 root history

7) Controlling History Size

Check the default buffer size ( HISTSIZE = 1000): $ echo $HISTSIZE Increase it for audit purposes: $ export HISTSIZE=10000 Note that HISTSIZE only limits the in‑memory buffer. To control the file size, set HISTFILESIZE : $ export HISTFILESIZE=200000 Persist these settings by appending to ~/.bash_profile and reloading:

$ echo "export HISTSIZE=10000" >> ~/.bash_profile</code>
<code>$ echo "export HISTFILESIZE=200000" >> ~/.bash_profile</code>
<code>$ source ~/.bash_profile

8) Changing the History File Name

Redirect history to a custom file using HISTFILE :

$ echo "export HISTFILE=/data/backup/chopin.bash_history" >> ~/.bash_profile</code>
<code>$ source ~/.bash_profile

9) Disabling History

Set both size variables to zero:

$ echo "export HISTSIZE=0" >> ~/.bash_profile</code>
<code>$ echo "export HISTFILESIZE=0" >> ~/.bash_profile</code>
<code>$ source ~/.bash_profile

10) Hacker’s Little Trick

Prefix a command with a space to prevent it from being recorded (requires HISTCONTROL to contain ignorespace ).

3. Summary

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

Common repeat shortcuts: !! , !-1 , !N , !string .

Interactive search: press Ctrl+R .

Key environment variables for safer history handling:

HISTSIZE : max entries in buffer

HISTFILESIZE : max entries in file

HISTIGNORE : patterns to exclude

HISTTIMEFORMAT : timestamp format

HISTCONTROL : options like ignorespace , ignoredups

Persist these variables in ~/.bash_profile for production environments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxproductivityhistoryShellcommand-lineBashaudit
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.