The blog provides Network Security Tips, Tricks, How To/Procedures. Products and areas not limited to Firewalls, Security, Check Point, Cisco, Nokia IPSO, Crossbeam, SecurePlatform, SPLAT, IP Appliance, GAiA, Unix/Linux.

Linux/Unix - How To View And Clear Command History

In Linux/Unix based system, command history of a session can be viewed using "history" command. Command history will be displayed with line number, highest line number is the last executed command.

Complete command history can be cleared using "history -c" command. Specific command line history can be deleted using command "history -d <line-number/offset>".

Syntax:

history

 

Usage:

history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]

 

Example:

[user@Linux]# history
    1  pwd
    2  ls -lh
    3  cd /var/log
    4  top
    5  history
[user@Linux]#

!! Delete line number 2 using command "history -d 2" !!

[user@Linux]# history -d 2
[user@Linux]# history
    1  pwd
    2  cd /var/log
    3  top
    4  history
    5  history -d 2
    6  history
[user@Linux]#

!! Clear complete command history using command "history -c" !!

[user@Linux]# history -c
[user@Linux]# history
    1  history
[user@Linux]#

!! Display timestamp using HISTTIMEFORMAT !! 
 
[user@Linux]# export HISTTIMEFORMAT='%F %T '
[user@Linux]# history | more

    1  2015-03-05 15:02:39 who
    2  2015-03-05 15:02:42 ls -l
    3  2015-03-05 15:02:50 pwd
    4  2015-03-05 15:02:52 export HISTTIMEFORMAT='%F %T '
    5  2015-03-05 15:02:53 history | more
[user@Linux]#

[user@Linux]# export HISTTIMEFORMAT=' '
[user@Linux]# history

    1  who
    2  ls -l
    3  pwd
    4  export HISTTIMEFORMAT='%F %T '
    5  history | more
    6  export HISTTIMEFORMAT=' '
    7  history
[user@Linux]#


Linux/Unix - How To View And Clear Command History Linux/Unix - How To View And Clear Command History Reviewed by Admin on 21:03:00 Rating: 5