> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Work on the Command Line Part 1 Use and edit command history

> Learn to list, search, and navigate your Bash command history to enhance command-line productivity.

In this lesson, you’ll learn how to list, search, and interactively navigate your Bash command history to boost your command-line productivity.

## 1. List Your Bash History

Bash keeps track of every command entered in a session. To display your entire session history, use:

```bash theme={null}
history
```

Example output:

```bash theme={null}
 1  history
 2  ls
 3  ls -la
 4  test
 5  clear
 6  cd
 7  pwd
 8  clear
 9  sudo systemctl reboot
10  ls -la
11  clear
```

## 2. Search Commands with grep

To filter history for specific keywords, pipe the output to `grep`. For example, to find all `sudo` invocations:

```bash theme={null}
history | grep sudo
```

Output:

```bash theme={null}
9  sudo systemctl reboot
```

<Callout icon="lightbulb" color="#1CB2FE">
  For comprehensive details on `grep`, see the [GNU grep Manual](https://www.gnu.org/software/grep/manual/).
</Callout>

## 3. Locate the .bash\_history File

Bash writes your commands to `~/.bash_history` when you log out. To view hidden files in your home directory:

```bash theme={null}
ls -la /home/aaron
```

Sample listing:

```bash theme={null}
drwx------ 14 aaron aaron  4096 Dec  5 13:52 .
drwxr-xr-x  3 root  root   4096 Dec  5 13:43 ..
-rw-------  1 aaron aaron   112 Dec  5 13:51 .bash_history
-rw-r--r--  1 aaron aaron    18 Nov 24 08:20 .bash_logout
-rw-r--r--  1 aaron aaron   141 Nov 24 08:20 .bash_profile
-rw-r--r--  1 aaron aaron   492 Nov 24 08:20 .bashrc
...
```

You can open `~/.bash_history` in any text editor or display it with:

```bash theme={null}
cat ~/.bash_history
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Commands executed in your current session only appear in `~/.bash_history` **after** you log out.
</Callout>

## 4. Navigate History Interactively

Bash lets you recall and edit past commands right in the prompt. Use these keystrokes:

| Keystroke      | Action                                 |
| -------------- | -------------------------------------- |
| ↑ (Up Arrow)   | Scroll backward to earlier commands    |
| ↓ (Down Arrow) | Scroll forward to more recent commands |
| Ctrl + R       | Reverse search through command history |

When you find the desired entry, press Enter to re-execute or edit it inline.

***

### Additional Resources

* [GNU Bash Reference Manual](https://www.gnu.org/software/bash/manual/)
* [Bash Command History Section](https://www.gnu.org/software/bash/manual/html_node/Bash-History-Builtins.html)
* [Bash Prompt Customization](https://linuxhint.com/bash_prompt_customization/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/linux-professional-institute-lpic-1-exam-101/module/2490f961-886c-4531-be8c-915cccff60a9/lesson/6791789b-52c3-4204-91c3-35419c2c3c4f" />
</CardGroup>
