2

At least in bash, by default, the option set +P is set, which makes certain commands like cd, pwd, and tab-completion follow the symbolic link hierarchy, rather than the physical hierarchy. But, lots of other commands, like ls, just follow the physical hierarchy.

You can do set -P to make all commands follow the physical directory structure (or add it to your ~/.inputrc file for permanence across sessions). But, what if I want to go the other way, to make all (most?) commands obey the symbolic directory structure? Can this be done? Is this bash-specific? Alternatively, is there a way to make things believe the symbolic directory is physical? And to be clear, I'm looking for a setting to affect behavior across all potential functions, not a workaround like readlink. The closest I can find is this workaround.

For an example of how one can get unpleasant results, keep reading. This can lead to confusing behavior since you can tab-complete to a file/folder that the command you're running won't be able to find. For instance (you might have to do set +P first):

mkdir symtest1
mkdir symtest1/subdir
ln -s symtest1/subdir
touch youreinthebasedir         #a lovely file to show where you're looking
touch symtest1/youreinsymtest1  #yet another
pwd                             #shows where you are
cd subdir
pwd                             #should show the same plus "/subdir"
ls ../                          #should show the contents of "symtest1"

Now, type the following with tab-completion,
ls ../youretab
and it'll complete to
../youreinthebasedir
but hit enter, and you'll get an error that the file doesn't exist due to the aforementioned discrepancy.

Some more useful discussion can be found here and here.

  • 1
    Note: POSIX defines parent directory as the directory that both contains a directory entry for the given directory and is represented by the pathname dot-dot in the given directory. Also here: The special filename dot-dot shall refer to the parent directory of its predecessor directory. A symlink is not "a directory entry for the given directory", so your shell interprets .. against POSIX; ls does it "right". – Kamil Maciorowski Oct 1 '18 at 20:17
  • 2
    Oh, the above comment is not meant to discredit the question. Your question is fine, I have upvoted it. I'm just giving some context and rationale. – Kamil Maciorowski Oct 1 '18 at 20:32
  • 1
    "Alternatively, is there a way to make things believe the symbolic directory is physical?" – You may find this interesting: What is a bind mount? – Kamil Maciorowski Oct 15 '18 at 14:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.