1

I’m trying to create a single relative symlink pointing public/train/train back to public/train, but my command ends up nesting infinitely. For example:

ln -snrf "$ROOT/public/train" "$ROOT/public/train/train" 

After running this, I see:

public/ └─ train/ ├─ train → ../train │ └─ train → ../../train │ └─ train → ../../../train │ … 

But I only want:

public/ └─ train/ └─ train → ../train 

I need to keep the -r option so that the link target is relative. How can I create exactly one public/train/train→public/train symlink without further nesting?

5
  • 1
    How are you getting the undesired output? Using tree? If so, which version? Commented Jun 4 at 9:29
  • SInce, as explained in Stephen's answer, it's impossible to create a symlink in a directory that's not there when you look into that directory: Maybe ask a question that describes what you need to achieve! It seems symlinks are not the solution, maybe the bind mount as suggested paladin is – but we can't know, because we don't understand why you need this! Commented Jun 4 at 12:12
  • by the way, no, it does not recurse infinitely, Linux (and very likely all other operating systems with symlink support) have a maximum redirection depth. Commented Jun 4 at 12:13
  • The -r option is only relevant if the link text is a relative path, it has no effect if $ROOT is absolute. Commented Jun 4 at 16:23
  • I think the link you want is created. It's just the tree command that has a problem with it. It should work for other purposes. E.g. if you have a file named public/train/foo, you should be able to access it using public/train/train/foo as well. Commented Jun 4 at 16:27

2 Answers 2

2

Technically, you can’t get what you want: train/train links back to the top-level train, which contains the train symlink, which links back to the top-level train, and so on.

The tree command on my system notices that the link is recursive, and stops:

$ tree train train └── train -> ../train 2 directories, 0 files 

and it refuses to dereference the symlink even when asked:

$ tree -l train train └── train -> ../train [recursive, not followed] 2 directories, 0 files 
1

The best thing you could do is using mount --bind.

Example:

cd public/train mkdir train chmod 000 train mount --bind . train 

This would allow only 1 times nesting for root, and probably no nesting at all for non root.

PS take also a look at What is a bind mount?

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.