FactorPad
Build a Better Process

Linux history Command Summary with Examples

Linux 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.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux history, 58 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / history-command.html


An ad-free and cookie-free website.


Examples of the Linux history command

Beginner

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

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux history Command Summary with Examples (3:37)

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.

Common Linux history Options
-options description
-c Clear the history list
!NUMBER Print the command at the line NUMBER

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.

(Why is the terminal screen 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?

If you like free opportunities to learn, subscribe, follow and tell a friend so the content keeps coming.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / history-command.html


linux history command
linux history
bash history
history linux
linux command history
unix history
history command
linux history examples
linux history file
clear terminal history
linux search history
delete bash history
linux history clear
linux history tutorial

A newly-updated free resource. Connect and refer a friend today.