Symbolic links to directories mystery.
Hi,
Although I'm quite at ease with command line and shell and fs (supposedly :) ), I just encountered a weird behavior which I am able to reproduce. I can't access some symlink from another symlink. Here it is. Code:
~$ mkdir tmp ; cd tmp Code:
~/tmp$ tree Sorry, I must have missed something stupid in my now long time ago education :). Thanks ! |
When you cd into symtoto, you are now actually in ~/tmp/contoto/toto. The directory above that is ~/tmp/contoto, which doesn't contain the symlink you want.
Try ls ../../symtata instead, or ls $OLDPWD/symtata. The shell is applying the logical directory structure, while the external program ls is looking at the actual physical directory structure. You can use set -P (a.k.a. set -o physical) to force bash to use the physical structure instead. man bash > set built-in Quote:
|
Oh! So you're telling me that ".." in a directory is actually a real link to the parent, and NOT a symbolic convention that work on the "path" (as a string of char). Alright, I didn't understood that. Thank you, I thought it was really an abstraction to build a "new path string" and not a real inode link.
Again, thank you for your fast answer :). Edit: Alright, I simply did what I should have done before posting: Code:
~/tmp$ cd symtata ; ls -l .. |
That's right. . and .. are not shell synonyms, but actual directories, as defined by the posix standard for filesystems (I believe). The shell just passes these directly to the command being executed, like any other file name.
It appears that the confusion mostly comes from tab completion, which is acting according to bash's logical directory structure, before the command is executed. So there's a discrepancy between the apparent an actual directory contents. This is probably something that needs to be fixed in the tab completion scripts. pwd/$PWD also displays the logical directory name, making it even harder to detect that you're in a symlinked directory. Running readlink -f $PWD will give you the real location. |
Quote:
Code:
$ pwd -P Code:
pwd [-LP] |
Hey, you taught me something too. I was not aware of (or forgot about) pwd -P. :)
It doesn't help with $PWD though, which I tend to use quite often in scripts. :( |
Maybe $(pwd -P) could do the job ? (I guess you have already thought about that ;) ).
Cheers. |
Yeah, of course you can do that. But it requires opening up a subshell to run the command, as opposed to simply reading a variable, so it's sub-optimal. I did once use readlink in the same way for a script that could encounter symlinked files.
But I don't usually use symlinks to directories myself, so most of my scripts are unlikely to be affected by this. It is a theoretical concern, however. |
All times are GMT -5. The time now is 04:27 PM. |