15
touch a
ln -s a b

The above command creates a symbolic link which originates from "b" and points to "a":

lrwxrwxrwx  1 root root     1 Dec 20 23:41 b -> a

The question is that how to say that correctly? If it is correct, then why is the reverse direction i.e "b" is the source and "a" is the target. And which entity is the symbolic link itself?

3 Answers 3

19

The syntax of ln is parallel to that of cp and mv.

cp a b

creates a file called b containing the content of file a.

ln –s a b

creates a symbolic link called b that points to a (i.e., provides access to the content of a).  Similarly,

ln a b

creates a hard link (a new directory entry) called b that refers to the file a (which must already exist, unlike the case of the symbolic link).  In all cases, the first argument is the source (of the data) and the second argument is the thing that the command creates.

6
  • 1
    Thank you. I originally thought not about the contents, but where it points: ln -s a<-b. That is why I called it "reversed". But now I understand.
    – dash17291
    Commented Dec 20, 2012 at 23:14
  • Id be careful with the talk about hard links. A symlink is a special filetype, so there is a source and a destination. A hardlink is just a second name for a file. The two hard links are equivalent. The second hard link does not refer to a, but to the same data that a refers to. a refers to the data, b refers to the data. There is no direction. Commented Dec 20, 2012 at 23:23
  • 3
    @Rich: OK, I should have said, “creates a hard link (a new directory entry) called b that refers to the file also referenced by the name (directory entry) a”. Commented Dec 20, 2012 at 23:35
  • 1
    For the life of me, I could never remember which order the files go in. It never occurred to me to think of it like cp or mv.
    – Pak
    Commented Aug 17, 2014 at 18:47
  • GNU cp even has -s to create symlinks instead of copying (and likewise -l for hard links).
    – deltab
    Commented Oct 31, 2014 at 3:16
0

Just remember that ls -l displays the link in reverse mode.

To know where a link point to, you can do :

$ file b
b: symbolic link to `a'
0

dash, you're interpreting the output wrong.

In the output of ls, think of the arrow, and its direction, as "points to".

Read b points at a. Makes b the symbolic link. Easy, really.

2
  • OK. According to man ln the first parameter is the target and the second is the link name which is to be created. I interpreted the target as the "source" like when using cp <source> <dest>, because I think of it that it is the one (in cp's or rsync's notation for example) which must exist before the operation and we specify that first.
    – dash17291
    Commented Dec 20, 2012 at 23:10
  • It is the source, similar to a copy operation, I assume it's called a "target" because the link will point at it. Makes sense to me, anyway. I still think you're just interpreting the output of ls wrong.
    – tink
    Commented Dec 20, 2012 at 23:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .