0

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.

7
  • How about pwd >> ~/.bash_history?
    – Philippos
    May 26, 2017 at 6:30
  • You mean making an alias of the what you are saying? Yes. May 26, 2017 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? May 26, 2017 at 6:39
  • this does it : pwd && pwd >> ~/.bash_history thanks. May 26, 2017 at 6:41
  • alias pwd='pwd | tee -a ~/.bash_history'
    – user680858
    May 26, 2017 at 7:28

2 Answers 2

0

Add this command to your .bash_rc:

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

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/
 popd

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 .