Fundamentals 4 min read

Master the Linux History Command: View, Edit, and Manage Your Shell Commands

This guide explains how to use the Linux history command to display past commands, manipulate the history list with options like clearing, appending, reading, writing, deleting entries, and provides practical examples and key notes for effective shell command management.

Open Source Linux
Open Source Linux
Open Source Linux
Master the Linux History Command: View, Edit, and Manage Your Shell Commands

history: View and Manipulate Command History

Feature Description

The history command displays previously executed commands and allows operations such as appending and deleting entries.

Command Syntax

history [options]

Option Meanings

-c

: Clear the current command history. -a: Append the current session's history to the history file. -r: Read the history file into the current session's memory buffer. -w: Write the current buffer to the history file. -d: Delete the command at the specified index from the history list. -n: Append new lines from the history file that have not yet been read.

Reference Examples

Example 1: Using History Commands

!!

– Execute the previous command. !3 – Execute the command numbered 3. !-2 – Execute the second-to-last command. !6 /test – Execute command 6 and append /test to it. !if – Execute the most recent command starting with if.

Example 2: Searching History Commands

– View the previous command. – View the next command. Ctrl+r – Incrementally search history by typing a keyword. Alt+p – Search for commands matching the typed characters.

Example 3: Common Commands

// (1) Show full history (shows number and command)
history

// (2) Show the last 5 commands
history 5

// (3) Write the current history buffer to the history file immediately
history -w

// (4) Clear the history list
history -c

Notes

The history is stored in memory and automatically saved or loaded when the shell exits or logs in. The number of stored commands is controlled by the HISTSIZE environment variable (default 1000).

When the terminal is closed, the history list is written to ~/.bash_history .

If the HISTTIMEFORMAT variable is set, its value is used as a format string for strftime(3) to prepend timestamps to each displayed entry; otherwise, no timestamps are shown.

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.

LinuxhistoryShellBashcommand-line
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.