Open In App
Related Articles

history command in Linux with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

history command is used to view the previously executed command. This feature was not available in the Bourne shell. Bash and Korn support this feature in which every command executed is treated as the event and is associated with an event number using which they can be recalled and changed if required. These commands are saved in a history file. In Bash shell history command shows the whole list of the command.

Syntax:

$ history 

Here, the number(termed as event number) preceded before each command depends on the system. You may get different numbers while executing on your own system.

Important Points

  • To show the limited number of commands that executed previously as follows:
    $ history 5 

    Note: The command can be executed using event number also.

    Example:

    $ !1997 

  • To print command before executing so that a wrong command does not get executed use :p after event number of command.

    Example:

    !1997:p

  • This command can also be used along with grep.

    Example:

    history | grep chpasswd 

  • The most recent command can be viewed using !!

    Example:

    !!

  • Suppose if the command has to be executed without being stored variable HISTFILE has to be unset.

    Example:

  • The command can also be executed using a part of the command.

    Example:

    !command_starting_string

  • History can also be removed using history -d event_number

    Example:

    history -d 1996 

  • The whole history can be removed using history -c option.

    Example:

    history -c

  • history | tail
  • It is used to view the last 10 commands
Last Updated : 05 Aug, 2022
Like Article
Save Article
Similar Reads
Related Tutorials