Linux History Command Tutorial with Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-history-command-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux history is a function of the bash that provides previously issued commands with different options. In this tutorial we will look these options.

Display History

To simple list history providing history command is enough. History file is located in users home directory and named .bash_history for bash shell.

Display History
Display History

OR

another way to display history in Linux. Keep in mind that this will display history without line numbers.

Display History With Timestamp

To get history commands with the timestamp for issue there is bash variable that changes timestamp.

Remove History Timestamp

To remove time stamp set HISTTIMEFORMAT bash variable to nothing. This will completely remove timestamp

Remove History Timestamp
Remove History Timestamp

Search History With Control+R

To search history for specific text Control+R shortcut can be used after Control+R write the text you want to search

Search History With Control+R
Search History With Control+R

We put text HI and get the command export HISTTIMEFORMAT=''

Run Previous Command

Do you remember what was our previous command? May be but history remembers it and we can call it with !! or !-1

Run Previous Command
Run Previous Command

After issuing !! the command which will be run is printed to the console and then run.

Execute Specific Command In The History

We may want to run specific command in the history. As we know history command by default provides number for each history entry. To run specific command in the history the number of the command can be used to specify the command.

LEARN MORE  Linux Mint

In this example we will run command number 1323 by using ! operator.

Execute Specific Command In The History
Execute Specific Command In The History

As expected the command is put to the terminal and then issued.

Execute Specific Command In the History With Specific Word

To rerun a command in the history by specifying the start of the command is like

Execute Specific Command In the History With Specific Word
Execute Specific Command In the History With Specific Word

As you see we have lastly run echo "poftut"command which starts with echo.

Change History Size Permanently

History size is by default 1000 line long after 1000 lines oldest command is removed from list. If we want to change the size of history we need to set new size trough HISTSIZE bash environment variable. To make the change permanent HISTSIZE can be added into .bash_profile file.

Disable Saving Duplicate Commands in History

While bashing there is a lot of repeating commands those have no meaning for us. Eliminating them will make history file more clear and less in size.

Erase Duplicate Commands in History

To remove existing duplicate commands we can use HISTCONTROL bash environment variable with erasedups value like below.

Force History Not Save Specific Command

Some junior system admins are trying to hide the commands they have issued. Here is how to do it. But keep in mind the best is not hide anyting

We have made space started command not visible for history. So we have issued ls command and then look into history but there is no record about it.

LEARN MORE  Tcp vs Udp

Clear All History

After making hack clear the history at least our commands 🙂

Clear All History
Clear All History

Disable History Completely

We have resized history in previous parts if we want to disable history completely we should set the size to zero.

Ignore Specific Commands From History using HISTIGNORE

If we wanted ignore some little from history using HISTIGNORE is the best way for to do.

Here we provide command by delimiting them with :

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-history-command-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.