MacOS – pwd in bash is wrong after sending directory to trash

bashfoldersmacos

If I move a directory to the trash that I'm already cd'd into that directory, pwd will report no change in location. I will still be able to do everything in bash normally as if there was no change in the directory, even though Finder says it doesn't exist.

pwd should always be accurate. This does not appear to be the case.

For example, I'm cd'd to this path:
/Users/me/Desktop/goingToTrash/subdir

If I move to trash /Users/me/Desktop/goingToTrash, pwd will report that I'm in /Users/me/Desktop/goingToTrash/subdir, even though the finder says it doesn't exist. So if I recreate the deleted directory, bash will be working from the old directory (I'm assuming in the trash, haven't verified yet) instead of the new one I just created, and pwd will provide no clue this happened.

Much worse, if I "move to trash" a directory and I'm cd'd to one of it's subdirectories, then recreate the directory structure, from a sub directory I can cd ../subdir, I'm magically back into the "new" directory, even though the parent was "moved to trash" instead of remaining in the directory structure that was moved to trash.

Best Answer

YMMV when there is an inaction between the shell and the Finder.app. It is best to use-

/bin/pwd

instead of the builtin pwd.

You can add this line to your ~/.bashrc and reset bash to "fix" this problem.

alias pwd="/bin/pwd"

An alternative to /bin/pwd (with or without the alias) is pwd -P. The -P option to the builtin pwd is explained in the output of help pwd.