Skip to content

history

16 commands

The shell built-in that lists, searches and re-runs previous commands, and manages the history file.

  • shell built-in
  • forensics

Viewing5

history

Numbered list of commands from this shell session and the history file.

history <n>

The last n commands.

Link
history | grep <text>

Searches past commands.

Link
Ctrl-R

Reverse incremental search: type part of a command and press Ctrl-R again for older matches.

HISTTIMEFORMAT="%F %T " history

Shows when each command was run (bash records times only if this was set when they ran).

Re-running5

!!

Repeats the last command — sudo !! reruns it as root.

!<n>

Runs command number n.

Link
!<prefix>

Runs the most recent command starting with the text.

Link
!$

The last argument of the previous command.

Link
^<old>^<new>

Reruns the previous command with a substitution.

Link

Managing4

history -c

Clears the in-memory history of this session.

history -d <n>

Deletes one entry.

Link
history -a

Writes this session’s commands to the history file now.

Link
<command>

With HISTCONTROL=ignorespace, a leading space keeps a command out of history.

Link

Forensics2

cat /home/*/.bash_history /root/.bash_history 2>/dev/null

Reads history files for every user. Empty or missing files, or a link to /dev/null, can indicate tampering.

Link
ls -la ~/.*history

Lists history files for different shells (bash, zsh, python, mysql, less).

Link