Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Apr 7 '13 at 10:24

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

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

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/

then, after ending your session, you could:

scriptreplay /tmp/ /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 )

if ! [[ "$(ps ho cmd $PPID)" =~ ^/usr/bin/script\ -t\ $HOME/.bash_script_ ]]
    function runScript() {
        local sname=$HOME/.bash_script_$(date +%F_%H-%M-%S_$$)
        exec /usr/bin/script -t 2> $sname.tim $sname
        exit 0

.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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .