WebMasterCampus
WEB DEVELOPER Resources

Linux history Command

Learn Linux history Command with examples


Linux history Command

In Linux, we can use “history” command lists and annotates the last 1000 commands issued in the terminal emulator. Each command has a number associated with it.

The GNU history command keeps a list of all the other commands that have been run from that terminal session, then allows you to replay or reuse those commands instead of retyping them.

history Command Syntax

>> history

history Command Example

Run the history command to see a list of the last 1000 commands. You’ll see that all the listed historical commands are given a unique reference number.

>> history
>> clear
>> ls
>> ls -l
>> sleep 10 &
>> history

The history command shows a list of the commands entered since you started the session.

The joy of history is that now you can replay any of them by using a command such as.

>> !2

The !2 command at the prompt tells the shell to rerun the command on line 2 of the history list.

Note: You can also use history to rerun the last command you entered by typing !!

history 15 Command Example

Reissue the history command but constrain the amount of results to a specific number.

This is useful if you know roughly when a command you are looking for was issued.

You should see only the last 15 results listed.

>> history 15

history | less

history | less to view just the last ten commands, you can use the following.

>> history | less

history | tail

history | tail to view the last 25 commands.

>> history | tail

Searching history

You can also use history to rerun the last command you entered by typing !!. By pairing it with grep, you can search for commands that match a text pattern or, by using it with tail, you can find the last few commands you executed. For example:

>> history | grep  "clear"

  136  clear
  141  clear -x
  142  man clear
>> history | tail -n 3

  94  ls | tail
  275  tail outout.txt
  276  tail output.txt
  277  tail output.txt -n 10

Another way to get to this search functionality is by typing Ctrl-R to invoke a recursive search of your command history.

Now you can start typing a command, and matching commands will be displayed for you to execute by pressing Return or Enter.

>> history | egrep -i 'scp|ssh|ftp'

history -c (delete all)

The -c option causes the history list to be cleared by deleting all of the entries:

>> history -c

CTRL + r

To get previous command containing string, hit [CTRL]+[r] followed by search string:

>> (reverse-i-search)
Created with love and passion.