Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
I use Zsh and I have setopt share_history in /etc/zsh/zshrc so this problem should never happen to me :P –  phunehehe Jan 20 '11 at 9:11
add comment

4 Answers 4

up vote 8 down vote accepted

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"
share|improve this answer
1  
This 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 5 '11 at 19:20
add comment

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.
share|improve this answer
1  
Unfortunately 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
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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