history

history linux command cheatsheet by Thamizhiniyan C S

Introduction

This command is used to display the history list with line numbers.


Syntax

history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]


Important Flags

FlagDescription

-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.


Executing Command without making an entry in history

Executing a command with space character " " as a prefix will get the job done.

# The following command won't make an entry in history since it is prefixed 
# with a space character
  echo "Some Kinda API Key"

Disabling and Enabling history

To prevent recording commands in the history list, temporarily disable recording by using:

set +o history

To re-enable recording, use:

set -o history

The commands have no output

Interactive Mode Commands

FlagDescription

ctrl-P

Move to the previous history entry. This is the same action as the up arrow.

ctrl-N

Move to the next history entry. This is the same action as the down arrow.

alt-<

Move to the beginning (top) of the history list.

alt->

Move to the end (bottom) of the history list, i.e., the current command line.

ctrl-R

Reverse incremental search. This searches incrementally from the current command line up the history list.

alt-P

Reverse search, non-incremental. With this key, type in the search string and press enter before the search is performed.

alt-N

Forward search, non-incremental.

ctrl-O

Execute the current item in the history list and advance to the next one. This is handy if you are trying to re-execute a sequence of commands in the history list.


Additional Features of history

FlagDescription

!!

Repeat the last command. It is probably easier to press the up arrow and enter.

![number]

Repeat history list item number.

![number]:p

Displays the command associated with event number without executing it.

!-[number]

Add a dash (-) before the command number to run a specific command from the end of the list.

![string]

Repeat last history list item starting with string.

!?[string]

Repeat last history list item containing string.

^[old string]^[new string]^

If you made a typo in a previous command, change it using this syntax. You can find a detailed example below


Examples

CommandDescription
history

Displays the command history.

history 5

Shows the last 5 commands from the command history.

!1997

Reruns the command with event number 1997.

!1997:p

Displays the command associated with event number 1997 without executing it.

!-7

To reuse the seventh last command

history | grep chpasswd

Filters and displays only the commands containing the term “chpasswd.”

!!

Reruns the last executed command.

!command_starting_string

Executes the latest command starting with “command_starting_string.”

history -d 1996

Removes the command with event number 1996 from history.

history -c

Clears the entire command history.

history | tail

Displays the last 10 commands from the history.


Customizing history output to show datetime stamps

Add the following line to the .bashrc file:

export HISTTIMEFORMAT="<DateTime_Format>"
Available Date Time FormatsDescription

%d

Day.

%m

Month.

%y

Year.

%H

Hour.

%M

Minutes.

%S

Seconds.

%F

Full date (Y-M-D format).

%T

Time (H:M:S format).

%c

Complete date and timestamp (Day-D-M-Y H:M:S format).

For example, To view the complete date and timestamp for each command in history, use the following format:

export HISTTIMEFORMAT="%c "

Now save the file and run the command source .bashrc or open a new terminal and run the history command to see the DateTime stamps.


Fixing Typos in Previous Command

If you made a typo in a previous command, use the following syntax to fix it:

^[old_string]^[new_string]^

References

Last updated