history
history linux command cheatsheet by Thamizhiniyan C S
Last updated
history linux command cheatsheet by Thamizhiniyan C S
Last updated
This command is used to display the history list with line numbers.
history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
Flag | Description |
---|---|
Executing a command with space character " " as a prefix will get the job done.
To prevent recording commands in the history list, temporarily disable recording by using:
To re-enable recording, use:
The commands have no output
Add the following line to the .bashrc
file:
For example, To view the complete date and timestamp for each command in history, use the following format:
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.
If you made a typo in a previous command, use the following syntax to fix it:
Flag | Description |
---|---|
Flag | Description |
---|---|
Command | Description |
---|---|
Available Date Time Formats | Description |
---|---|
-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.
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.
!!
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
%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).
Displays the command history.
Shows the last 5 commands from the command history.
Reruns the command with event number 1997.
Displays the command associated with event number 1997 without executing it.
To reuse the seventh last command
Filters and displays only the commands containing the term “chpasswd.”
Reruns the last executed command.
Executes the latest command starting with “command_starting_string.”
Removes the command with event number 1996 from history.
Clears the entire command history.
Displays the last 10 commands from the history.