History command
history
- GNU History Library
The history
command in Linux is a shell built-in (typically in bash
) that displays the command history of your current session. It shows a list of previously executed commands, allowing you to review, reuse, or manage them.
Note: history
is tied to your shell (usually bash
), stored in ~/.bash_history
, and requires no special privileges beyond your own session.
Usage: history [options] [n]
n
: Optional number of recent commands to display.options
: Flags to modify behavior (e.g., clear, delete).
Common Options
Option | Description |
---|---|
-c | Clear the current history list |
-d n | Delete command number n |
-w | Write current history to file |
-r | Read history file into current session |
-a | Append current session to history file |
Examples
-
Basic Usage
Run
history
to see all commands executed in your current shell session.history
- Output (example):
1 ls -l
2 cd /home/user
3 cat file.txt
4 history - Each line has a number (command ID) and the command itself.
- Output (example):
-
Limiting Output
Specify a number to show only the last
n
commands.history 3
- Output:
2 cd /home/user
3 cat file.txt
4 history - Shows the last 3 commands.
- Output:
-
Reusing Commands
Use
!n
to re-run a command by its history number.!3
- Executes
cat file.txt
(if 3 was that command).
Last Command:
!!
- Repeats the most recent command.
- Executes
-
Searching History
Press
Ctrl+R
in the terminal to search interactively.- Type
Ctrl+R
, thencat
:(reverse-i-search)`cat`: cat file.txt
- Press
Enter
to run, orCtrl+R
again to cycle through matches.
Alternatively, use
history | grep
:history | grep "cd"
- Finds commands containing "cd".
- Type
-
Clearing History
Use
-c
to erase the current session’s history.history -c
- Clears the in-memory history (doesn’t affect
~/.bash_history
until logout).
Clear and File:
history -c && history -w
- Clears memory and overwrites
~/.bash_history
with an empty list.
- Clears the in-memory history (doesn’t affect
-
Writing to File
Use
-w
to save the current session’s history to~/.bash_history
.history -w
- Updates the history file immediately (normally done on logout).
-
Deleting Specific Commands
Remove a command by its number with
-d
.history -d 2
- Deletes command number 2 from the history.
- Check:
history
shows updated list.
To get help related to the history
command use --help
option
$ history --help
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
Display or manipulate the history list.
Display the history list with line numbers, prefixing each modified
entry with a `*'. An argument of N lists only the last N entries.
Options:
-c clear the history list by deleting all of the entries
-d offset delete the history entry at position OFFSET. Negative
offsets count back from the end of the history list
-a append history lines from this session to the history file
-n read all history lines not already read from the history file
and append them to the history list
-r read the history file and append the contents to the history
list
-w write the current history to the history file
-p perform history expansion on each ARG and display the result
without storing it in the history list
-s append the ARGs to the history list as a single entry
If FILENAME is given, it is used as the history file. Otherwise,
if HISTFILE has a value, that is used, else ~/.bash_history.
If the HISTTIMEFORMAT variable is set and not null, its value is used
as a format string for strftime(3) to print the time stamp associated
with each displayed history entry. No time stamps are printed otherwise.
Exit Status:
Returns success unless an invalid option is given or an error occurs.
For more details, check the manual with man history