History Command Examples In Linux

To check history of the commands which has been executed.

[root@server ~]# history
    1  whoami
    2  clear
    3  ls
    4  whoaami
    5  ls -ltr
    6  top -c

To check last 5 commands in history use history 5

[root@server ~]# history 5
   92  clear
   93  whoami
   94  top -c
   95  history
   96  history 5

To clear history use history -c

[root@server ~]# history -c
[root@server ~]#
[root@server ~]# history
    1  history

To delete a particular command in history use history -d

[root@server ~]# history 5
1 history
2 whoami
3 clear
4 hisry 5
5 history 5

here I'm deleting the command which I fired at 4th Number
[root@server ~]# history -d 4
[root@server ~]# history
1 history
2 whoami
3 clear
4 history 5
5 history -d 4
6 history

To run a command at particular number in history you can use ! followed by the number. Example you want to run the command at number 2 the run !2

[root@server ~]# !2
whoami
root

Leave a Reply

Your email address will not be published. Required fields are marked *