Display timestamp in command history

Source: Internet
Author: User
Article Title: displays the timestamp in the command history. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

History is a very practical shell command that can display the commands run in the shell before. With the last command, you can trace which user has executed some commands.

Last
...
Mysurface tty7: 0Mon Oct 6 20: 07-Down ( 00: 00 )
Reboot system boot 2.6 . 24 . 4 -64. Fc8 Mon Oct 6 20: 06 ( 00: 00 )
Mysurface pts/ 8 10.168 . 28 . 44Mon Oct 6 17: 42-Down ( 01: 58 )
Mysurface pts/ 7: 0.0Mon Oct 6 17: 41- 19: 40 ( 01: 59 )
Mysurface pts/ 6: 0.0Mon Oct 6 17: 27- 19: 40 ( 02: 13 )
Mysurface pts/ 5: 0.0Mon Oct 6 17: 27- 19: 40 ( 02: 13 )
Mysurface pts/ 5: 0.0Mon Oct 6 15: 52- 15: 59 ( 00: 07 )
...

However, by default, history does not record the command execution time, which is difficult to analyze. At this time, you can configure the HISTTIMEFORMAT environment variable to add the timestamp to the history record:

Export HISTTIMEFORMAT = "% F % T"

 

Effect:

...
994 2008 -10 -16 02: 27: 40 Exit
995 2008 -10 -16 01: 12: 20Iptables-nL
996 2008 -10 -16 01: 47: 46Vi. bash_profile
997 2008 -10 -16 01: 47: 55 History
998 2008 -10 -16 01: 48: 03.. Bash_profile
999 2008 -10 -16 01: 48: 04 History
  1000 2008 -10 -16 01: 48: 09 Exit
  1001 2008 -10 -16 02: 27: 43 History
...

 

Add the above export command ~ /. Bashrc or ~ /. In bash_profile, The history Timestamp can be automatically enabled after logon ~

In fact, the format of HISTTIMEFORMAT is the format of the strftime function, such as the above "% F % T", % F Represents the date that shows the Y-M-D format, % T represents the time that shows the format of H-M-S. For more formats, refer to man strftime.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.