2

Consider a symlink I am making to my Music directory named music.

ln -s Music music

now consider following sequence of commands:

edward@ArchLinux:~$ readlink music
Music
edward@ArchLinux:~$ readlink music/
edward@ArchLinux:~$ 

I am getting output only if I am not using / at the end of symlink name.

I wonder if /dir and /dir/ are different. Can anybody explain?

2
  • 3
    Yes. /dir is the symlink. /dir/ is the resolution of that link.
    – mikeserv
    Commented Jan 20, 2016 at 11:34
  • @Gilles the question you suggested is a duplicate of a duplicate of the one I linked.
    – cat
    Commented Jan 20, 2016 at 13:14

2 Answers 2

5

readlink expects a symbolic link, and then displays the file/dir that symbolic link points to.

  • in your first attempt: it sees the symbolic link, so it displays what it points to
  • in your second attempt: it sees music/., which is Music/. which is the pointed directory, not the symbolic link pointing to that directory, so it doesn't have a link to interpret. (In other words, when you add the final "/", the shell's file descriptor is for the pointed directory instead).
2

On Unix, everything is a file descriptor.

In this case, "everything" includes normal files, directories, partitions (e.g. /dev/sda1), devices (e.g. /dev/sda), virtual devices (e.g. /dev/null) and symlinks.

But what is a "file descriptor" in this case? It's an entry in your file system root, pointing to the data location on your disk or in the memory (for virtual files). Note that e.g. on ext?-filesystems, there's another layer of abstraction between file descriptor and data called "inode".

So in your example, music is a symlink, which is represented by a file descriptor, just like any other file or directory. But it has a special flag set to identify it as symlink, where a directory would have another flag set defining it as such one. Now the system can interpret the symlink, which is actually just a string containing an absolute or relative path to the target.

It gets only interpreted as directory if you explicitly write a / behind it, otherwise it's treated as file.

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