My current screen
session has 12 open windows on it. It's been running for weeks... I know I executed an ImageMagick convert
command in one of these 12 screen windows sometime last week... is there any way I can easily search through the Bash history of all 12 instances, without closing them or running history | grep convert
in all 12 screens?

- 829,060

- 8,449
4 Answers
You can run history -a; history -c
in all windows to save the history. And then history -r
to refresh it.
To solve it more permanently add this to your .bashrc
:
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

- 9,994
-
2This is great, thanks! The only problem is that if you type something in one shell and then go to another and type history immediately, it won't show up in the history. This is because the PROMPT_COMMAND has not executed yet, so the refresh has not occurred. You'll have to hit return in the second window (or run another command) first, then type history. – Javid Jamae Jul 05 '11 at 19:20
tcsh allows you to share history between sessions through use of the periodic alias.
In your .cshrc (or .tcshrc) file:
set tperiod = 60 #(or any other number. Time in minutes between refreshing)
alias periodic 'history -S; echo "Syncing history"'
This will automatically execute the history command every 60 minutes. This will save history to a common history file in your home directory. To update the history more often, either lower the tperiod number, or simply execute the "periodic" command to manually refresh

- 21
- 3
Sounds difficult. Here are a couple of methods that may work for you.
If you have process accounting tools installed (on Linux, look for a package called acct
) and the permission to use them, you can find out when and possibly on what terminal you ran convert
:
lastcomm convert
If this is unavailable or unconclusive, you can execute a history
command in each instance of bash to look for convert
commands. This will find commands that were in the history file when bash started as well.
: >/tmp/convert.history
for w in $(seq 0 11); do
screen -p $w -X stuff \
'history | sed 's/^.*convert/'$w'&/" >>/tmp/convert.history
'; done
$(seq 0 11)
iterates over the numbers of your screen windows. Make sure to skip windows that are not currently running bash but some other process that would interpret input differently.screen -p $w stuff …
sends the following string as input to the specified window. You need the newline at the end of the string.- The file
/tmp/convert.history
will contain a list of lines like3 convert foo.jpg
, if you ranconvert foo.jpg
in window 3.

- 829,060
-
1Unfortunately these didn't work. :-( I just ended up running
history | grep convert
in all windows. I am disappointed, I wanted to accept another great answer by Gilles! – Josh Jan 21 '11 at 15:08
Bash only writes the history when it exits, which makes this problematic at best. I've heard that zsh can share history between active sessions.

- 39,666
- 4
- 75
- 104
setopt share_history
in/etc/zsh/zshrc
so this problem should never happen to me :P – phunehehe Jan 20 '11 at 09:11