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?

Alex Jones
  • 6,353

2 Answers2

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.