Take the 2-minute tour ×

There is a cute way of loggin the start date/time of all commands in linux console. You should set the variable HISTTIMEFORMAT to "%F %T ". Then by running 'history' command you will see something like:

  512  2011-09-02 22:57:41 export HISTTIMEFORMAT="%F %T "
  513  2011-09-02 22:57:42 ls
  514  2011-09-02 22:57:43 hist
  515  2011-09-02 22:57:45 history 

This is very cool and useful. But my dream is to add to this log also the command execution time. I know that I can run 'time ./some_long_lasting_script' but I don't want to write time every time manually. Maybe there is some way of auto saving every command execution time?

share|improve this question
Which shell do you use? bash? zsh? And what exactly do you want in your log file - there are myriads of time output formats like xxx 0,00s user 0,00s system 0% cpu 0,001 total or something like: real 0m0.007s user 0m0.000s sys 0m0.004s What exactly do you want (which timings) and in what format? –  GreyCat Sep 3 '11 at 2:18
I'm using bash. The ideal time is rounded seconds. –  bessarabov Sep 4 '11 at 2:40
There are at least 3 possible timings - usually "user time", "system time" and "wall (total) time". Which one would you want? –  GreyCat Sep 4 '11 at 6:04
Any time will suit me. I need this to record time of long lasting commands, and I don't need super accuracy. –  bessarabov Sep 4 '11 at 19:18

1 Answer 1

If it wasn't too distracting, you could change your prompt to display the current date and time.

If you're using bash, consider something like:

export PS1="[\D{%Y-%m-%d} \T][\u@\h \W]\$ "

which would look like:

[2011-09-03 03:39:21][james@fractal ~]$ echo $PS1
[2011-09-03 03:39:30][james@fractal ~]$ vi
[2011-09-03 03:39:39][james@fractal ~]$ ping google.com
PING google.com ( 56(84) bytes of data.
64 bytes from qw-in-f147.1e100.net ( icmp_req=1 ttl=47 time=51.3 ms
--- google.com ping statistics ---
rtt min/avg/max/mdev = 51.343/51.343/51.343/0.000 ms
[2011-09-03 03:39:44][james@fractal ~]$ 

You could, of course, tailor your prompt to your personal preferences.

To permanently change your prompt you can add that export PS1=... to your ~/.bashrcfile. Alternatively, you could write aliases to toggle back and forth, for example, in your ~/.bashrc file,

alias prompt_ts_on='export $PS1=...'
alias prompt_ts_off='export $PS1="[\u@\h \W]\$ "'

Note that ... should be your choice of double-quote enclosed prompt string.

Some references on PS1 escape sequences: here and here

share|improve this answer
The problem with this solutin that time in prompt shows the time when the promt was created instead of command run time. –  bessarabov Sep 4 '11 at 2:42

Your Answer


