12. Revisiting your History history, !

Phew! That was a lot of commands we learned! In case you wanted to look back at your command history, the shell allows you to do so! In fact, for each session, the shell stores everything you typed!

Looking through your command history

To see the contents of the history list, simply use the history command.

$ history 

For a better format, pipe the results to less.

$ history | less 

Furthermore, we can pipeline certain commands with grep. Recall that grep searches for lines containing a certain pattern.

$ history | grep ls

Going back to a specific history

Each command is listed in chronological order (1 being your very first command). To go back to a specific command, simply place the command number after an exclamation mark (!).

$ !3

Searching through the history list

If you recall a few keywords from a command you executed, but don't recall it all, you can easily to a search through the history of your commands with ^r (ctrl+r).

After the command you want pops up, press ^j to copy to your current line.

Toggling back and forward through your history list

If you want to toggle up and down your history list, you can do so with the up and down arrow keys. Additionally, there is ^p and ^n, which stand for previous and next.

Additional shortcuts

Here are some additional history shortcuts.

Repeats the last command. This is especially useful when you forget to sudo in before running a command!
Repeats the last history item starting with string
Repeats the last history item containing string

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources