FactorPad
Faster Learning Tutorials

Linux history command summary with examples

history allows you to review command history.
  1. Purpose - Learn what history is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with history.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, October 31, 2016
Updated: August 4, 2018
In this tutorial, 58 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

~/ home  / tech  / linux essentials  / history command

Outline Back Next


The Linux history command

Beginner

Learn to review and access command history at the Linux command line.

Video Tutorial

Linux history command summary with examples (3:37)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our fifty-eighth word, or command to memorize is history from our category System.

history allows you review command history.

Recall from video 28, we used the tail command to look at my video 56 on bash command history? Here we'll explore functionality in the history command.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The history command has 8 options and the argument can be a line number, file name or search string.

Unlike most commands, help is not available with double-dash --help as history is a shell built-in (builtin), so use the manuals page for both history and bash.

history has a few advanced search features, but for us -c clears the history, and the exclamation plus a number !NUMBER pulls that line number.

So why is history an important command? Well, sometimes you remember you once had a stroke of genius and need to hit the rewind button. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's start with history optionless.

$ history
1999 exit 2000 clear 2001 tail -n 20 .bash_history 2002 history 2003 man history 2004 history 2005 type history 2006 which history 2007 man history 2008 clear 2009 man bash 2010 clear 2011 history --help 2012 man history 2013 clear 2014 which history 2015 man bash 2016 history 2017 clear 2018 PS1="\n \$ " 2019 clear 2020 ls 2021 ls -al 2022 clear 2023 pwd 2024 ls 2025 ls -al 2026 clear 2027 history

Mine shows the last thousand. You can set how much history you'd like to save.

(Want a clue as to why the terminal screen is black on this video? Look at the date of recording ;)

Next, let's use a tail command with -n and 5 to see the last 5 lines in the file called .bash_history, where this history is kept.

$ tail -n 5 .bash_history pwd ls flkclear clear exit $ clear

Notice these two don't match. Well the history command shows more recent entries, in this session and appends them to the history file on logout. So if you need to see the most recent, use the history command.

And last, let's pick a few random numbers and see my history. This could be scary. (I wouldn't suggest trying this at home, it is called history expansion and it executes previous commands. This can be helpful if you have a long command you don't want to take the time to type. It can be dangerous to do randomly like this, depending on what you've been doing ;)

$ !1503 $ !1967 tail -n 20 .bash_history clear pwd clear history tail -n 5 .bash_history clear less video48.txt pwd clear pwd ls pwd less video48.txt history man history pwd ls flkclear clear exit $ !2016 2003 man history 2004 history 2005 history 2006 which history 2007 man history 2008 clear 2009 man bash 2010 clear 2011 history --help 2012 man history 2013 clear 2014 which history 2015 man bash 2016 history 2017 clear 2018 PS1="\n \$ " 2019 clear 2020 ls 2021 ls -al 2022 clear 2023 pwd 2024 ls 2025 ls -al 2026 clear 2027 history 2028 tail-n 5 .bash_history 2029 clear 2030 tail -n 20 .bash_history 2031 history

I need to get a life.

A Final Tip

Okay now you know how to use history. And you know the syntax for commands, options and arguments.

One last tip about the history command. So searching in history has its own syntax. You may find it easier in a text editor you know.

Okay, thanks for visiting today. I hope this was a helpful introduction to the history command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Use the YouTube Channel to send us a question. Here's a subscription button.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the vim command, hit Back.
  • To cover the important kill command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / history command



 
 
Keywords:
linux history command
history Linux
linux command history
bash history
unix history
linux history file
linux history clear
linux erase history
linux history expansion
history shell built-in
linux search history
delete bash history
linux history tutorials
linux history examples