1

I recently decided it would be a good idea to start backing up my history, and did so with the following added to .bashrc:

 export HISTFILE=~/.history/`date +%Y%m%d`.hist
 export HISTSIZE=100000

No problem, seems to do what it says it should do.

However I noticed today (after my first log back in to the shell since setting it) that my actual history as recalled by history (or when grepping for patterns within) itself, was only showing my commands since that log in, it was as if the backup had also erased .bash_history.

When I inspected .bash_history manually with less, it was all there as I had expected it should be.

i.e., the output of a tail to history is:

wms_joe@Esau:~$ history | tail
48  past dir
49  ll
50  less .bash_history
51  tail .bash_history
52  cp .bash_history history_backup
53  past usearch
54  less ~/.bashrc
55  history
56  tail history
57  history | tail

But a tail to .bash_history gives:

wms_joe@Esau:~$ tail .bash_history
cd prot_all/
ls
find . -type f -name *.txt -print0 | xargs -0 sed -i /XBW1/d
find . -type f -name '*.txt' -print0 | xargs -0 sed -i /XBW1/d
less PAU_03392_out.txt
less ../../PVC_operons/prot_all/PAU_03392_out.txt
past cut
less PAU_03392_out.txt
cut -f3 PAU_03392_out.txt | bc
less PAU_03392_out.txt

And these are files etc. I know I was working on recently, and their command IDs should be up in to the 4000-5000 region, not 50 or so.

So my question being, how to I make history look at the right thing again? Or can I somehow merge the contents of .bash_history back in to the history so that it is recalled by the command correctly?

closed as unclear what you're asking by G-Man, PersianGulf, jimmij, Jakuje, garethTheRed Apr 3 '16 at 15:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Try setting your history file in .bashrc back to the right location – John Militer Apr 2 '16 at 22:52
  • Note you can explicitly read or write a history file with history -r filename and history -w filename. See also options -a and -n. – meuh Apr 3 '16 at 6:39
  • basically you change history file to a day base history file, wait a day, re-log, and now history has changed. That waht you asked for. – Archemar Apr 3 '16 at 9:50
  • I was following a guide on some other page, but I didn't realise it would be 'resetting' the history file. I though it was basically just writing a copy of it each day, not actually changing the focus of history. – Joe Healey Apr 3 '16 at 10:58
  • @John has got the idea spot on. All I had to do was export HISTFILE=~/.bash_history and it's back to normal. I'll rethink my backup approach when I come to look at it again. – Joe Healey Apr 3 '16 at 11:00
1

You must set HISTSIZE=0 to close the old $HISTFILE before making your changes. See man bash.

Not the answer you're looking for? Browse other questions tagged or ask your own question.