2

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.

8
  • 2
    are you actually typing cp ~/. bash_history $path/filename.txt or cp ~/.bash_history $path/filename.txt I'm not seeing a very big difference between my .bash_history and history
    – jesse_b
    Commented Aug 9, 2017 at 12:50
  • 1
    One thing I believe may be the issue is it appears history doesn't write to .bash_history until you log out. So .bash_history should include everything except your current session, where history would include everything.
    – jesse_b
    Commented Aug 9, 2017 at 12:52
  • Please edit your question and i) clarify the command you use. The one you show would give you an error. ii) Explain what's missing. Which command in the history don't you see? Is it only the latest ones from your current shell (open terminal)? iii) What format do you want the time and date to be? Show us an example of your expected output.
    – terdon
    Commented Aug 9, 2017 at 12:55
  • 1
    Alternatively you can use history -a; cp ~/.bash_history $path/filename.txt to ensure that your bash_history is written to just before you perform the copy.
    – jesse_b
    Commented Aug 9, 2017 at 13:09
  • The bash in-memory history is flushed to the history file at the end of the shell session, by default. You would have to manually flush the history before copying the file. The bash manual contains all relevant information.
    – Kusalananda
    Commented Aug 9, 2017 at 13:09

2 Answers 2

4

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: "
2
  • Thanks again for your help. I ran history -a to flush the current history and then ran my bash file which included cp ~/.bash_history $path/filename.txt and it did include the most up to date history. Is there a way to automate this process as part of my bash file rather than having to type history -a
    – JOrchard
    Commented Aug 9, 2017 at 13:25
  • This process is already automated when you log out. Is there another action you would like to initiate this process?
    – jesse_b
    Commented Aug 9, 2017 at 13:45
1

To get date and the time in your history , run the following commands:

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

How to see time stamps in bash history

Why this doesn't give me the complete history?

See the answer of @michael-homer

If you want to make sure that they're always written immediately, you can put that command into your PROMPT_COMMAND variable:

export PROMPT_COMMAND='history -a'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .