Can I easily search my history across many screen windows?

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?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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"

Method 2

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 like 3 convert foo.jpg, if you ran convert foo.jpg in window 3.

Method 3

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

Method 4

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.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments