> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Introduction to History Command in Linux [For Beginners]
- URL: https://linuxhandbook.com/history-command/
- Published: 2020-05-22T16:27:55.000Z
- Updated: 2024-09-18T06:18:28.000Z
- Description: Everything you type in the terminal is stored in the shell history. Learn how to use command history in Linux in this introduction to history command.
- Author: Christopher Murray
- Tags: Commands

Let’s face it, us humans are not great at remembering things. Sometimes being forgetful can be very frustrating and lead to painful consequences.

Unlike us, computers are capable of near-perfect memory. One example is the history command in Linux. This program keeps track of everything that we type into the command line.

It sounds pretty simple, and it is. You’d be surprised how often this tool can come in handy, though!

Let’s walk through some examples that will improve our command-line skills and make us more productive.

## Where are the settings for history command stored in Linux?

To understand the history command better, we will look at what settings can be configured by the user.

The typical default for the number of commands logged is 1000 on [Ubuntu](https://ubuntu.com/?ref=linuxhandbook.com), but it can vary from system to system. The settings for history are set by your bash profile (Normally \~/.bashrc). There are several history file settings that are editable from this file including the location of the log file.

By default, command history is usually logged to \~/.bash\_history.

```
christopher@linuxhandbook:~$ cat ~/.bash_history
sudo apt-get clean
#sudo e4defrag /
df -h
sudo e4defrag /
sudo fstrim /
sudo -s
poweroff
ls
```

## How to use the history command in Linux?

![History Command](https://linuxhandbook.com/content/images/2020/06/history-command.png)

Let’s see some examples of using history command in Linux.

### 1) Use arrow keys to scroll through history of commands

Many of us don’t think of what the command line was like before we knew this trick. If you don’t already know, using your up/down arrow keys lets you quickly scroll through your command history.

This is great for when you make a mistake while entering some long complex command. Just hit the up arrow, and correct it. Done.

### 2) Default history settings

Let’s start with something simple. We’re just going to enter the command and see what is returned.

```
history
```

This will return a numbered list of your recently entered commands.

```
christopher@linuxhandbook:~$ history
    1  sudo apt-get clean
    2  #sudo e4defrag /
    3  df -h
    4  sudo e4defrag /
    5  sudo fstrim /
    6  sudo -s
    7  poweroff
    8  ls
    9  history
```

Looks familiar, right? This is the content from our .bash\_history file, plus a new entry for ‘history’.

In fact, new entries will not be reflected in the file until you close the shell.

### 2) Running a command from the history using index

Did you notice that the commands were numbered when you ran the history command? To re-run a specific command quickly, type `!` and then the number.

```
christopher@linuxhandbook:~$ !8
ls
Desktop    Downloads         fontconfig  Pictures  Templates
Documents  examples.desktop  Music       Public    Videos
```

### 3) Delete a command from history

If you made a mistake, you can erase a specific item with the -d option followed by the line number. Let’s erase line number nine.

```
christopher@linuxhandbook:~$ history -d 9
christopher@linuxhandbook:~$ history
    1  sudo apt-get clean
    2  #sudo e4defrag /
    3  df -h
    4  sudo e4defrag /
    5  sudo fstrim /
    6  sudo -s
    7  poweroff
    8  ls
```

### 4) Delete entire command history

Maybe you want to get rid of more than one line. We can erase the entire history using the -c option.

```
christopher@linuxhandbook:~$ history -c
christopher@linuxhandbook:~$ history
    1  history
```

### 5) Searching for commands in the history

Obviously, my history file is now fairly short since I’ve just cleared it. However, with the potential to have up to 1000 commands, things can get messy quickly. Fortunately, you can use tools to search history to help find that complex command you used earlier.

#### Method 1\. Use Grep

Here’s an updated list of our history.

```
christopher@linuxhandbook:~$ history
    1  history
    2  ls
    3  ll
    4  cd
    5  cd ..
    6  cd 
    7  ls
    8  ll
    9  history
```

Now, I am going to use the history command piped with the [grep command](https://linuxhandbook.com/grep-command-examples/) to find entries containing the character `l`.

```
christopher@linuxhandbook:~$ history | grep l
    2  ls
    3  ll
    7  ls
    8  ll
   10  history | grep l
```

#### Method 2: Use reverse-i-search

You can also search your commands using the reverse-i-search tool. If you hit Ctrl+r, you will see a new prompt that you can use to search your command history.

```
christopher@linuxhandbook:~$ 
(reverse-i-search)`': 
```

You can start typing and it will return results as you add letters. This is one of the [must-know terminal shortcuts in Linux](https://linuxhandbook.com/linux-shortcuts/).

Both grep and the reverse-i-search can be used to make your life easier.

## **Conclusion**

This was just the very basic history command examples. There are no ends to customizing it as per your preference and requirement. If it interests you, you should know about these [bash history tips](https://linuxhandbook.com/bash-history-tips/).

[5 Simple Bash History Tricks Every Linux User Should KnowEffectively using bash history will save you plenty of time in the Linux terminal.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookAbhishek Prakash![](https://linuxhandbook.com/content/images/2021/07/Bash-history-tips.png)](https://linuxhandbook.com/bash-history-tips/)

I hope you find this introduction to history command useful. Please provide your feedback in the comment section.