Operations 5 min read

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.

php Courses
php Courses
php Courses
Managing Linux Command History: View, Search, Modify, and Clear

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:

echo "HISTSIZE=2000" >> .bash_profile

echo "HISTFILESIZE=2000" >> .bash_profile
source ~/.bash_profile

To display timestamps alongside each entry, set the format variable: export HISTTIMEFORMAT="%F %T" 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:

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

To clear the entire history, run: history -c 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
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.

LinuxShellSysadminBashterminalcommand history
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

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.