Javatpoint Logo
Javatpoint Logo

Linux history Command

Linux history command is used to display the history of the commands executed by the user. It is a handy tool for auditing the executed commands along with their date and time.

Most of the commands read input from the terminal a line at a time. But, the history command is capable of keeping the record of those lines with associated data. By default, it will show the last five hundred commands from the older to most recent commands. The history library is saved in a history file.

Syntax:

The basic syntax for the history command is as follows:

Options:

The following are some command-line options that are supported by the history command:

-c: It is used to clear the complete history list.

-d offset: It is used to delete the history entry at the position OFFSET.

-a: It is used to append history lines.

-n: It is used to read all history lines.

-r: It is used to read the history file.

-w: It is used to write the current history to the history library.

-p: It is used to perform history expansion.

-s: It is used to append the ARGs to the history list as a single entry.

How to use the history command?

The basic use of the history command is pretty straight forward. Execute the history command as follows:

It will list the previously executed command from the history library. Consider the below snap of output:

Linux history Command

From the above output, a list of 500 commands is displayed.

Display the nth command from the history

We can display the specific number of commands by specifying it as "!<command number>". For example, we want to show the most recent command which is 500th in our history file, execute the command as follows:

It will display the most recent command from our history. Consider the below output:

Linux history Command

From the above output, we have executed the man command, so it is showing it. The output will be different according to history.

Display the n Recent Commands

We can list the last n commands by using the history command. For example, if we want to display the previous five commands, execute the below command:

The above command will display the previous five executed commands. Consider the below output:

Linux history Command

Display the Command by Specifying its Name

We can display the command by specifying its name. It will display the most recent use of that command. For example, i want to see the last "ls" command. To do so, execute the command as follows:

The above command will display the previous use of ls command from history. Consider the below output:

Linux history Command

Display the Command with a Search Pattern

We can display the history of command by specifying a search pattern. To specify a pattern, pipe the history command with grep command as follows:

The above command will search for the commands which match the pattern 'se'. Consider the below output:

Linux history Command

Changing an Executed history Command

We can turn an executed history command by returning a command with different syntax. For example, if we want to change our previous command "history | grep se" with "history | grep ma" . To do so, execute the below command:

The above command will change the previous history commands behavior. It will display the commands matching with the pattern "ma" instead of "se." Consider the below output:

Linux history Command

From the above output, all the commands matching with pattern "ma" are displayed.

Removing History

The history command allows us to remove the data from the history library. We can remove a particular line or complete history.

To remove a particular command, execute the history command by specifying the command number in history as follows:

For example, to delete the most recent command, execute the below command:

The above command will delete the most recent command, which is on number 500. Consider the below output:

Linux history Command

From the above output, the 500th command from the history is deleted. It will not return anything if successfully executed.

To remove the complete history of executed command, run the below command:

The above command will delete the entire history from the history library.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA