Linux History Command Tutorial with Examples – POFTUT

Linux History Command Tutorial with Examples


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.

$ history
Display History
Display History

OR

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

$ cat ~/.bash_history

Display History With Timestamp

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

$ export HISTTIMEFORMAT='%F %T '

Remove History Timestamp

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

$ export HISTTIMEFORMAT=''
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

(reverse-i-search)`HI': export HISTTIMEFORMAT='%F %T '
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.

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

$ !1323
Execute Specific Command In The History
Execute Specific Command In The History

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

LEARN MORE  Linux fsck Command Tutorial With Examples

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

$ !echo
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.

$ echo HISTSIZE=100000 >> .bash_profile

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.

$ export HISTCONTROL=ignoredups

Erase Duplicate Commands in History

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

$ export HISTCONTROL=erasedups

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

$ echo "HISTCONTROL=ignorespace" >> .bash_profile  
$ export HISTCONTROL=ignorespace

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.

Clear All History

After making hack clear the history at least our commands 🙂

$ history -c
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.

$ echo "HISTSIZE=0" >> .bash_profile

Ignore Specific Commands From History using HISTIGNORE

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

$ echo "HISTIGNORE='pwd:ls:ls -ltr:'" >> .bash_profile             
$ export HISTIGNORE='pwd:ls:ls -ltr:'

Here we provide command by delimiting them with :

LEARN MORE  Linux wc Command Word and Line Count Tutorial With Examples

Linux History Command Tutorial with Examples Infographic

Linux History Command Tutorial with Examples Infographic
Linux History Command Tutorial with Examples Infographic

 

Leave a Comment