The Linux history Command

The history command is used to display the list of previously typed commands. While the base command will list all previous commands used since the last clearing, it can also be tailored to display by the page or a certain number of commands.

Command: history
Synopsis: history [option]

Options:

OptionDescription
-cClears the history log by deleting all entries.
-d offsetDeletes entry at the offset. For example: history -d 8 will delete the entry at line 8 in the history log. All entries are then renumbered.

Examples

history – The base command lists all the commands that were previously entered.

 # history     1  (06/10/14) 13:43:41 clear     2  (06/10/14) 13:43:42 cd /     3  (06/10/14) 13:43:47 ping google.com     4  (06/10/14) 13:43:56 tail -f /var/log/messages     5  (06/10/14) 13:44:05 cd ..     6  (06/10/14) 13:44:11 cd /home/userna5     7  (06/10/14) 13:44:14 history     8  (06/10/14) 13:44:39 cd public_html     9  (06/10/14) 13:44:44 touch test.html    10  (06/10/14) 13:45:02 history   

history n – Using the base command plus n where n is the number of lines you want to display. Below is an example displaying the last 5 commands.

 # history 5     7  (06/10/14) 13:44:14 history     8  (06/10/14) 13:44:39 cd public_html     9  (06/10/14) 13:44:44 touch test.html    10  (06/10/14) 13:45:02 history    11  (06/10/14) 14:05:06 history 5   

history -d n – The delete offset command allows you to delete a specific command from the list. The number used in the command relates to the line number you want to delete. Below is an example where we delete the 9th line number. Notice that in the last history list that command is no longer present.

 #history
1 (06/10/14) 13:43:41 clear 2 (06/10/14) 13:43:42 cd / 3 (06/10/14) 13:43:47 ping google.com 4 (06/10/14) 13:43:56 tail -f /var/log/messages 5 (06/10/14) 13:44:05 cd .. 6 (06/10/14) 13:44:11 cd /home/userna5 7 (06/10/14) 13:44:14 history 8 (06/10/14) 13:44:39 cd public_html 9 (06/10/14) 13:44:44 touch test.html 10 (06/10/14) 13:45:02 history # history -d 9 # history 1 (06/10/14) 13:43:41 clear 2 (06/10/14) 13:43:42 cd / 3 (06/10/14) 13:43:47 ping google.com 4 (06/10/14) 13:43:56 tail -f /var/log/messages 5 (06/10/14) 13:44:05 cd .. 6 (06/10/14) 13:44:11 cd /home/userna5 7 (06/10/14) 13:44:14 history 8 (06/10/14) 13:44:39 cd public_html 9 (06/10/14) 13:45:02 history 10 (06/10/14) 14:16:38 history -d 9 11 (06/10/14) 14:17:07 history

history -c – This command clears the history list.

 # history -c # history    1  (06/10/14) 14:21:04 history   

history | tail – This history command will list the last 10 commands used. Note that the character between history and tail is a pipe character, not a lowercase ‘L’.

history | tail    13  (06/10/14) 14:58:23 ls -l    14  (06/10/14) 14:58:27 clear    15  (06/10/14) 14:58:31 history    16  (06/10/14) 14:58:41 history | less    17  (06/10/14) 15:00:23 history | less    18  (06/10/14) 15:05:03 history | tail    19  (06/10/14) 15:12:51 clear    20  (06/10/14) 15:12:52 man pwd    21  (06/10/14) 15:13:03 man cd    22  (06/10/14) 15:16:36 history | tail   

0 thoughts on “The Linux history Command

  1. Hi,

    Do you know how I can reset the history offset?

    Whenever I do “history -S” my offset increases so that now the history list does not start from “1”.

    How can I reset the list (without deleting it) so that it will start from “1” again?

     

    Thank you.

Was this article helpful? Join the conversation!