Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I often navigate between multiple directories in a bash session. Sometimes upto 4. For each directory I am in I often have an exclusive set of commands that I fire and this set can be between 7 and 10 commands. I was wondering if it is possible to create an association between my bash command history and the cwd in which those commands were fired. Appreciate any suggestions.

share|improve this question
    
can you give an example? why don't you use pwd? (with backquotes) –  m4573r Sep 10 '12 at 14:45
    
For example: cd adir -> cmd_for_a1, cmd_for_a2 cmd_for_a3, ... followed by cd bdir -> cmd_for_b1,... and so on. This means that my command history is clearly associated with where I am in terms of working dir. I just want to make it easy for me to bring back those commands based on my cwd. –  Ketan Sep 10 '12 at 15:03
1  
In that case, wouldn't it be easier to have a script in your path that woulc issue different commands considering the `pwd` where you launched it from? –  m4573r Sep 10 '12 at 15:14

2 Answers 2

up vote 1 down vote accepted

Use the following re-definition for cd and and put these lines into .bashrc:

            HISTFILE=$HOME/.bash_history.`echo $PWD|sed -e 's/\//_/g'`
            cd () {
                history -a
                command cd "$@"
                HISTFILE=$HOME/.bash_history.`echo $PWD|sed -e 's/\//_/g'`
                history -c
                history -r
            }
share|improve this answer

You can use PROMPT_COMMAND to check $PWD and take appropriate action, something like this:

PROMPT_COMMAND='if [[ $PWD =~ /usr/share ]]; then echo take action in /usr/share; fi'
share|improve this answer

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.