History Command in Linux

History command is a very common and one of the frequently used Linux built-in command in daily operation. This command will show you the commands history that have been executed, also help to recollect forgotten command. 

There is also a whole lot more that you can be do with history command. The history of command are stored in the ~/.bash_history file by default for each users. In this article, we’ll demonstrate how to use history command effectively with examples.
Read Also: Linux Commands Every Linux System Administrator Should Know With Examples Part-1
Read Also: Linux Commands Every Linux System Administrator Should Know With Examples Part-2

Syntax:
$history

1. Print History (Shows All executed commands)
This will simply shows the bash command 
history from the terminal of the current user logged in with line numbers. Newer commands at the bottom and old commands are at the top. By default 1000 lines of history will be saved.
# history

2. Search the History using Ctrl+r
This is the most frequently used feature of history command. If you have type really long command and can not remember it, you can simply search history  command typing a keyword with Ctrl+r (small letter ‘r’). Press Tab or Enter key to execute selected command or ESC to cancel it. The prompt will change as shown below and you can start searching old executed command

3. Repeat Most Recent Command in 4 different ways.
Sometime you have to run previous command. Followings are the 4 different ways to repeat last command executed.

  • Use up arrow
  • Typing !! and press Enter.
  • Typing !-1 and press Enter.
  • Press Ctrl+p will display previous command and press Enter to execute. 

4. Re-execute specific command
As shown in point no. 1. above, history command displays the line numbers. It’s also possible to repeat a command by specifying line number with history command. As shown below screenshots we have execute line number 14 command.
#!14


5. Pipe History
You can pipe the output of the history command into many other commands such as less or grep. You can narrow down for searching old command, outputting it to grep.
# history | grep -i httpd

6. Delete Specific Line number
You can delete a specific line number from the history command. In below, line number 237 will be deleted.
# history -d 237

7. Timestamp with History Command
By default, you won’t be able to see the date and time that the commands were executed, you can set the $HISTTIMEFORMAT variable with a date and time format.

# export HISTTIMEFORMAT=’%F %T ‘

8. Find History file and Increase size of history.
By default 1000 lines of command will be saved in history. You can modified .bash_profile (hidden file) file add below two lines. And execute . .bash_profile (hidden file) to accept changes. By default, history is stored in ~/.bash_history (hidden file)file.

# vi ~/.bash_profile
HISTSIZE=100
HISTFILESIZE=100
Check current size of history and history stored in location with below commands.
echo $HISTSIZE  
echo $HISTFILE

9. Disable storing History of commands.
In some cases, you are not allow to keep or store command history, we can disable editing .bash_profile (hidden file) and enter below line.
HISTSIZE=0
# vi ~/.bash_profile
HISTSIZE=0


10. List specific user’s executed commands.
How to view command history executed by a specific user. Bash shell stores records of history in a ~/.bash_history (hidden file) by default. User can see commands executed in this file but there won’t be line numbers.
# cat .bash_history

That’s it. In this post, we have seen how effectively we can use history command in Linux. Please share it with others and follow us on our Facebook Official Page.

Leave a Comment