Bash – History with bash file

bashcommand history

I'm running a bash file to obtain data on what system processes are running (i.e netstat, syslog, arp etc). All's going well except for the history.
I'm trying to save the history to a text file with

cp ~/. bash_history $path/filename.txt   

Unfortunately this doesn't give me the complete history. If manually input the command

history > /media/root/usb/filename 

I get a completely different result.
My aim is to automate the whole process under the one bash file, is there a way of doing this. Also i'd ideally like the date and time with the history.

Best Answer

As @Jesse_b commented, .bash_history contains the history of all your previous shell sessions except the current one, which can be examined via the historycommand. So you'll have first to flush the current history via

history -a

and then you can copy it to another file:

cp ~/.bash_history $path/filename.txt   

To prepend each history entry with a timestamp, set the $HISTTIMEFORMAT variable, e.g.

HISTTIMEFORMAT="%F %T: "
Related Question