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.

Sometimes I like to look back in the command history to figure out what I was doing. Sometimes it would help to know what the cwd was for each command.

So, I'm wondering if there's some way to track this. I guess it's not too terrible to just dump the cwd in with the history in the history file, though it would have a lot of redundancy. So maybe it is better to configure it so that as part of rendering the prompt I can get zsh to write the current history index and the cwd into some other log. That is sufficient to store the information for use later.

share|improve this question

migrated from stackoverflow.com Apr 7 '13 at 10:24

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers

As far as I am aware, this would be something you would have to write yourself. Things might get a little confusing if your file hierarchy changes at all though. Anyway, this link might help you if you decide to write this yourself.

share|improve this answer
add comment

If you're just looking in history file, you have to search for cd commands that stand before the command you watch, for understanding the correct execution location.

If you wanna track pwd in your history file, you will have to write some tracker.

Some ways to do it simply:

.0 Before of all, a simple way to find a location from your .bash_history:

grep <.bash_history '^cd \|theSpecificCommandISearchFor'

.1 script You could initiate a shell session by typing:

script -a /tmp/script-XXXX -t 2>/tmp/script-XXXX.tm

then, after ending your session, you could:

scriptreplay /tmp/script-XXXX.tm /tmp/script-XXXX

to see all what's was done.

This solution is generic, must work finely with every interactive commandes line tool (not only bash, nor shell, you could even save vim session with this tool).

You could add this at top of your .bashrc (if you use )

# THIS WILL LOG EVERYTHING ON A TYPSCRIPT FILE USING script TOOL! # 
if ! [[ "$(ps ho cmd $PPID)" =~ ^/usr/bin/script\ -t\ $HOME/.bash_script_ ]]
  then
    function runScript() {
        local sname=$HOME/.bash_script_$(date +%F_%H-%M-%S_$$)
        exec /usr/bin/script -t 2> $sname.tim $sname
        exit 0
    }
    runScript
  fi

.2 use trap debug to create an alternative history file, like:

trap 'echo "$PWD :: $BASH_COMMAND" >>/tmp/my_trapDebug_history' debug

Nota: This work under , I don't know if nor how this work with other shell interpreter, like zsh or dash.

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.