This question already has an answer here:

I want to know how I can include the current working directory (output of pwd) to the commands stored in .bash_history file.

I want this because sometimes same command is executed from different directories. It's really helpful to know from which directory a particular instance of a command was executed.

marked as duplicate by Sergiy Kolodyazhnyy, user364819, Charles Green, Sumeet Deshmukh, Eric Carvalho May 27 '17 at 19:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • How about pwd >> ~/.bash_history? – Philippos May 26 '17 at 6:30
  • You mean making an alias of the what you are saying? Yes. – Bajarang Sutar May 26 '17 at 6:31
  • Umm.. Creating an alias of pwd >> ~/.bash_history as pwd does send the full path to the ~/.bash_history but I would like to retain the output on the terminal also. How can I do that? – Bajarang Sutar May 26 '17 at 6:39
  • this does it : pwd && pwd >> ~/.bash_history thanks. – Bajarang Sutar May 26 '17 at 6:41
  • alias pwd='pwd | tee -a ~/.bash_history' – user680858 May 26 '17 at 7:28

Add this command to your .bash_rc:

alias pwd='pwd | tee -a ~/.bash_history' 

As other have suggested, writing your current directory to your .bash_history can get you back to it.

But on the fly, see if pushing the current directory on to the stack and poping back to it later might be what you need.

 pushd . 
 cd /else/where/

you can also push as many directories. Poping them one at a time will revert the working directory back in the pushed order.

  • pushd . saves the current directory (don't forget the dot .),
  • cd changes working directory,
  • popd brings you back to the last pushed (saved) directory.

These commands will also show you the folder you have saved in their respective order every time you push or pop. and when entering the command history, you will be able to track back the changes made in each directory afterwards.

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