Reconstruct a bash_history file
Polytropon
freebsd at edvax.de
Thu Nov 2 20:46:45 UTC 2017
On Thu, 2 Nov 2017 15:13:50 -0400, James B. Byrne via freebsd-questions wrote:
> I wish to reconstruct a bash_history file for a user who has
> HISTTIMEFORMAT=%F %T:. I have a history log file having the following
> format:
> . . .
> 2014-06-19 16:09:25: whois playford.de
> 2014-06-20 08:24:00: man xpdf
> 2014-06-23 08:22:16: su -l
> 2014-06-24 09:33:27: cd Desktop
> 2014-06-25 09:22:25: su -l
> 2014-06-26 12:15:28: su -l
> 2014-06-27 08:48:40: su -l
> 2014-06-30 08:18:04:
> 2014-07-02 15:40:57: history | grep enscript
> 2014-07-03 08:38:55: ll
> 2014-07-04 09:29:04: su -l
> 2014-07-07 09:33:34: ssh -Y xnet241
> 2014-07-08 08:29:59: su -l
> 2014-07-09 06:53:46: su -l
> 2014-07-10 11:20:20: whois arcom.com.br
> 2014-07-11 10:02:24: su -l
> 2014-07-14 09:58:27: su -l
> 2014-07-15 08:46:38: su -l
> 2014-07-16 10:31:52: sshtn xnet241
> 2014-07-17 09:50:23: history | grep PORT
> . . .
>
> Is there a way to recreate a valid bash_history file from this data
> and preserve the time stamps?
Yes.
> If so then how?
You need the YYYY-MM-DD HH:MM:SS timestamp to the Epoch format,
prefix it with a #, and put the command on a new line.
Maybe like this, if you don't mind a multiple-line one-liner
of regular shell script:
$ cat history.txt | while read LINE; do DATETIME=`echo $LINE | cut -d ':' -f 1-3`; TIMESTAMP=`date -j -f "%Y-%m-%d %H:%M:%S" "${DATETIME}" "+#%s"`; COMMAND=`echo $LINE | cut -d ':' -f 4-`; echo "${TIMESTAMP}"; echo "${COMMAND}" | sed "s/^ //"; done > bash_history.txt
This version is easier to read:
cat history.txt | while read LINE; do
DATETIME=`echo $LINE | cut -d ':' -f 1-3`
TIMESTAMP=`date -j -f "%Y-%m-%d %H:%M:%S" "${DATETIME}" "+#%s"`
COMMAND=`echo $LINE | cut -d ':' -f 4-`
echo "${TIMESTAMP}"
echo "${COMMAND}" | sed "s/^ //"
done > bash_history.txt
It features the "useless use of cat" line the one-liner. ;-)
For your example input text, I get the following result:
#1403186965
whois playford.de
#1403245440
man xpdf
#1403504536
su -l
#1403595207
cd Desktop
#1403680945
su -l
#1403777728
su -l
#1403851720
su -l
#1404109084
#1404308457
history | grep enscript
#1404369535
ll
#1404458944
su -l
#1404718414
ssh -Y xnet241
#1404800999
su -l
#1404881626
su -l
#1404984020
whois arcom.com.br
#1405065744
su -l
#1405324707
su -l
#1405406798
su -l
#1405499512
sshtn xnet241
#1405583423
history | grep PORT
Does this look correct?
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
More information about the freebsd-questions
mailing list