0
~/temp$ mkdir dir1
~/temp$ mkdir dir2
~/temp$ mkdir dir2/dir21
~/temp$ ln -s dir2/dir21 dir1/ln2dir21
~/temp$ mkdir dir1/ln2dir21/dir3
mkdir: cannot create directory ‘dir1/ln2dir21/dir3’: No such file or directory

What does

~/temp$ ln -s dir2/dir21 dir1/ln2dir21

create (there are no errors for the ln command)? The created link dir1/ln2dir21 is red and it's type is "lrwxrwxrwx" which seems to be a link. Then why can't create directory through that symbol link?

  • Have you tried reading the manual page? ie. man ln ? – guiverc Jan 18 '20 at 12:44
  • 2
    Does this answer your question? Question about the symlink of the ln command – guiverc Jan 18 '20 at 12:45
  • I'v read them, but there are too much like form 1/2/3/4 so not very carefully... – jw_ Jan 18 '20 at 12:47
  • dir1/ln2dir21 symbolic link is relative to dir1. The correct command would be ln -s ../dir2/dir21 dir1/ln2dir21. – FedonKadifeli Jan 18 '20 at 12:47
  • man ls tells me "ln [OPTION]... [-T] TARGET LINK_NAME" ie. TARGET is first so that's the target of the command, LINK_NAME is second which is the link (-s tells it symbolic) created. SPACES are delimiters, ie. "dir2/dir21" is the TARGET and "dir1/ln2dir21" the LINK_NAME in format path/file (where file can be the name of a file or a directory (file contains more files; in posix/unix/linux everything is a file in a way) – guiverc Jan 18 '20 at 12:50
1

The dir1/ln2dir21 symbolic link you created is relative to dir1.

The correct command would be:

ln -s ../dir2/dir21 dir1/ln2dir21

As another test, if you go to dir1 and create dir2/dir21 you will see that the red indicator will go away:

cd dir1
mkdir -p dir2/dir21
ll

You will see ln2dir21 -> dir2/dir21/ in normal color (no red error color).

1

~/temp$ mkdir dir1/ln2dir21/dir3 you can't create a directory in a directory that is inexistent use mkdir -p

ln -s dir2/dir21 dir1/ln2dir21 is not working, because you're a) linking to a file not a dirrectory and b) it should be a full path. https://stackoverflow.com/a/9104390

so it should be: ln -s ~/temp/dir2/dir21/ ./dir1/ln2dir21

and it should workl...

  • I'm linking to a file, not directory. – jw_ Jan 18 '20 at 12:59

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.