Let's say we have ./a and ./b directories. Then we create a symlink using ln -s a x and entering it cd x The pwd would be x

Then in another terminal we change the x so that it pointed to b. But the first one would still return x for pwd which is not consistent anymore.

So is there a way for current terminal (bash) to alert/warn if the current path is the obsolete symlink?


Here's an approach which uses the fact that bash does command expansion on $PS1 to test to see if . and $PWD have the same inode in the prompt:

PS1='$([[ $(stat -Lc%i $PWD 2>/dev/null) == $(stat -c%i .) ]] || echo "\[\e[1;31m\]PWD invalid or changed \[\e[m\]")'"$PS1"

I don't believe there is any way to accomplish this with just regular Bash. You could setup inotify so that it would watch a given directory and then notify the user, typically through email, but there are likely ways to pipe these messages back into a user's shell/environment.

But these notifications would be broad in the sense that they would just come to the users when the links were changed/broken, they wouldn't be contextual, i.e. they wouldn't be sent only when the user was actual in these said directories.


Terminal #1:

$ mkdir $HOME/tst
$ inotifywait -mr $HOME/tst
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

Terminal #2:

$ mkdir somedir

Terminal #1:

/home/saml/tst/ CREATE,ISDIR somedir
/home/saml/tst/ OPEN,ISDIR somedir
/home/saml/tst/ CLOSE_NOWRITE,CLOSE,ISDIR somedir

Terminal #2:

$ rmdir somedir

Terminal #1:

/home/saml/tst/ DELETE,ISDIR somedir
/home/saml/tst/somedir/ DELETE_SELF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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