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
Flag | Description |
---|---|
| Clear the history list by deleting all of the entries. |
| Delete the history entry at position OFFSET. Negative offsets count back from the end of the history list. |
| Append history lines from this session to the history file. |
| Read all history lines not already read from the history file and append them to the history list. |
| Read the history file and append the contents to the history list. |
| Write the current history to the history file. |
| Perform history expansion on each ARG and display the result without storing it in the history list. |
| 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.
Disabling and Enabling history
To prevent recording commands in the history list, temporarily disable recording by using:
To re-enable recording, use:
The commands have no output
Interactive Mode Commands
Flag | Description |
---|---|
| Move to the previous history entry. This is the same action as the up arrow. |
| Move to the next history entry. This is the same action as the down arrow. |
| Move to the beginning (top) of the history list. |
| Move to the end (bottom) of the history list, i.e., the current command line. |
| Reverse incremental search. This searches incrementally from the current command line up the history list. |
| Reverse search, non-incremental. With this key, type in the search string and press enter before the search is performed. |
| Forward search, non-incremental. |
| 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
Flag | Description |
---|---|
| Repeat the last command. It is probably easier to press the up arrow and enter. |
| Repeat history list item number. |
| Displays the command associated with event number without executing it. |
| Add a dash (-) before the command number to run a specific command from the end of the list. |
| Repeat last history list item starting with string. |
| Repeat last history list item containing string. |
| If you made a typo in a previous command, change it using this syntax. You can find a detailed example below |
Examples
Command | Description |
---|---|
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. |
Customizing history output to show datetime stamps
Add the following line to the .bashrc
file:
Available Date Time Formats | Description |
---|---|
| Day. |
| Month. |
| Year. |
| Hour. |
| Minutes. |
| Seconds. |
| Full date (Y-M-D format). |
| Time (H:M:S format). |
| 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:
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:
References
Last updated