> For the complete documentation index, see [llms.txt](https://thamizhiniyancs.gitbook.io/cheatsheets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thamizhiniyancs.gitbook.io/cheatsheets/linux/knowing-about-files-commands/history.md).

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

```bash
# 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:

```bash
set +o history
```

To re-enable recording, use:

```bash
set -o history
```

The commands have no output

## Interactive Mode Commands

| Flag     | Description                                                                                                                                                         |
| -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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

| Flag                          | Description                                                                                                  |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------ |
| `!!`                          | 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

<table><thead><tr><th>Command</th><th>Description</th></tr></thead><tbody><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">history
</code></pre></td><td>Displays the command history.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">history 5
</code></pre></td><td>Shows the last 5 commands from the command history.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">!1997
</code></pre></td><td>Reruns the command with event number 1997.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">!1997:p
</code></pre></td><td>Displays the command associated with event number 1997 without executing it.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">!-7
</code></pre></td><td>To reuse the seventh last command</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">history | grep chpasswd
</code></pre></td><td>Filters and displays only the commands containing the term “chpasswd.”</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">!!
</code></pre></td><td>Reruns the last executed command.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">!command_starting_string
</code></pre></td><td>Executes the latest command starting with “command_starting_string.”</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">history -d 1996
</code></pre></td><td>Removes the command with event number 1996 from history.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">history -c
</code></pre></td><td>Clears the entire command history.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">history | tail
</code></pre></td><td>Displays the last 10 commands from the history.</td></tr></tbody></table>

***

### Customizing history output to show datetime stamps

Add the following line to the `.bashrc` file:

```bash
export HISTTIMEFORMAT="<DateTime_Format>"
```

| Available Date Time Formats | Description                                           |
| --------------------------- | ----------------------------------------------------- |
| `%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:

```bash
export HISTTIMEFORMAT="%c "
```

<figure><img src="https://4233913354-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fd5ZS6Rs98abkExqzELYB%2Fuploads%2FYQBLpPin8Ut0hhoAv0Go%2Fimage.png?alt=media&amp;token=c4a6ff7d-295a-43bc-b3f3-ef2f8f57d6a0" alt=""><figcaption></figcaption></figure>

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:

```bash
^[old_string]^[new_string]^
```

<figure><img src="https://4233913354-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fd5ZS6Rs98abkExqzELYB%2Fuploads%2FRoDYNLmdPyGUhPuYiqXY%2Fimage.png?alt=media&amp;token=f9f2a35e-f6a9-478b-8ec6-3fe66ab71f7f" alt=""><figcaption></figcaption></figure>

***

## References

{% embed url="<https://phoenixnap.com/kb/linux-history-command>" %}

{% embed url="<https://www.geeksforgeeks.org/history-command-in-linux-with-examples/>" %}
