:::: MENU ::::

Display time with history command

  • Apr 07 / 2017
  • 0
Linux

Display time with history command

When you are using the history command on a UNIX system, by default, you’re only getting an action list with numbers but no date and/or time to complete this history. For that, you have to define an environment variable HISTTIMEFORMAT that will describe how you want to display the timestamp for each command.

To display the timestamp temporarily (during session), you can just export the variable before sending the command:

$export HISTTIMEFORMAT="%d/%m/%y %T " ; history

You will get:

2021  01/04/17 11:54:04 systemctl list-units
2022  01/04/17 11:54:10 systemctl list-unit-files
2023  01/04/17 11:54:15 cd /etc/
2024  01/04/17 11:54:33 ls -lsta
2025  01/04/17 11:54:33 ps auxw | grep apache
2026  01/04/17 11:54:35 date
2027  01/04/17 11:54:35 cd ..
2028  01/04/17 11:54:37 cd /var/log
2029  01/04/17 11:54:37 ls -lsta
2030  01/04/17 11:54:40 cat messages
2031  01/04/17 15:48:44 history
2032  01/04/17 15:48:51 export HISTTIMEFORMAT="%d/%m/%y %T " ; history

If you want to keep that display for a future session, you have to add this variable to your bash profile and reload your profile:

$echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
$source ~/.bash_profile

Comments are closed.

Question ? Contact