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/
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?

  • Try setting your history file in .bashrc back to the right location Commented Apr 2, 2016 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
    Commented Apr 3, 2016 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
    Commented Apr 3, 2016 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
    Commented Apr 3, 2016 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
    Commented Apr 3, 2016 at 11:00

1 Answer 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 .