Score:1

Saving the date to actual bash history file

mx flag

When adding "HISTTIMEFORMAT" to bashrc, the timestamps of when a command was executed are made available when running the "history" command.

But the timestamps themselves are not saved to the bash_history file (atleast not in plain text).

I am looking for a solution that will write the timestamp to the file itself so that archived .bash_history files from various workstations can be viewed in an editor outside the userspace and still contain the timestamps of when commands were executed.

If the timestamps are being saved to the history file itself but just not viewable in an editor when opening the bash_history file, and it is still possible to view these timestamps by using the history command itself on a rogue bash history file, then that would also suffice.

Thanks

fo flag
Just adding the variable to the bashrc is not enough: you need to source the bashrc in each of your open shells to "activate" the variable.
fo flag
And the history file is only written/appended when a shell exits.
Score:1
lb flag

This is a example of bash history ( when using HISTTIMEFORMAT )

#1625963751
sleep 45 ; ssh proxy.lan

the number 1625963751 is the unix timestamp ( https://en.wikipedia.org/wiki/Unix_time ) .

if you are using linux , date has a option to display this date in human form .

em444$ date -d @1625963751
Sat Jul 10 20:35:51 EDT 2021
OB7DEV avatar
mx flag
How do I append the unix timestamp to each command in a bash file? When opening old bash historys I have, there is no #number above each command as there is in your example. Opening a bash_history file in vim just gives a list of commands. Each line is a command, there's no time stamps above each line.
EchoMike444 avatar
lb flag
if you dont use HISTTIMEFORMAT , you will not have timestamp .
Score:0
vn flag

edit /etc/bashrc and add this line to the bottom:

PROMPT_COMMAND="echo `date +'%F %H:%M'` `whoami` `history|tail -1` >> ~/history.txt; $PROMPT_COMMAND"

I am not sure if you change ~ to root, \root\history.txt can be accessible to all users yet.

Gerard H. Pille avatar
in flag
\root\history.txt ???
George Y avatar
vn flag
`~/history.txt` means the file `history.txt` in current user's home directory. `/root/history.txt` means the file in root directory. I assume you want to save historical commands to a specific file if you want a timestamp on each commands.
Gerard H. Pille avatar
in flag
It's only that backslashes have a different meaning on unix. Apart from that, 'history -1' may be cheaper than piping through tail.
OB7DEV avatar
mx flag
It seems I cannot get the history|tail -1 output appended to history.txt. The rest of the command works though. So my history.txt has date, user, but no command...
Score:0
vn flag

export HISTTIMEFORMAT='(%Y-%m-%d) (%H:%M:%S) ' Try this line.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.