Managing Linux Command History: View, Search, Modify, and Clear
This article explains how to use the Linux history command to view past commands, search with Ctrl +r, repeat previous entries, adjust the number of stored records, and clear the history, providing practical examples and configuration tips for effective shell management.
When developing on Linux, re‑typing long commands can be tedious; the up‑arrow only shows recent entries, making it hard to retrieve older commands, especially after closing an Xshell session.
The history command lists the last 1000 commands by default, and you can increase this limit by editing HISTSIZE and HISTFILESIZE in .bash_profile :
<code>echo "HISTSIZE=2000" >> .bash_profile
echo "HISTFILESIZE=2000" >> .bash_profile
source ~/.bash_profile</code>To display timestamps alongside each entry, set the format variable:
<code>export HISTTIMEFORMAT="%F %T"</code>Now history shows when each command was executed.
Search the history interactively with Ctrl +r . For example, typing usr after pressing Ctrl +r will locate the command usr/local/nginx/sbin/nginx -t used to test an Nginx configuration.
Repeat the most recent command using the up‑arrow, !! , or !-1 . To execute a specific entry by its number, use !1020 (replace 1020 with the desired index shown by history ).
The history size can be adjusted as shown earlier; the variables control how many entries are displayed ( HISTSIZE ) and how many are stored in the history file ( HISTFILESIZE ). Example entries after modification:
<code> 1039 2021-03-02 22:29:37 echo "HISTSIZE=2000" >> .bash_profile
1040 2021-03-02 22:29:58 echo "HISTFILESIZE=2000" >> .bash_profile
1041 2021-03-02 22:30:20 source ~/.bash_profile</code>To clear the entire history, run:
<code>history -c</code>Summary
View history with history
Search with Ctrl +r
Repeat last command using !! or !-1
Execute a specific entry with !<em>n</em> (e.g., !1020 )
Adjust displayed count via HISTSIZE
Adjust stored count via HISTFILESIZE
Clear history with history -c
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.