2

When is a symlink treated as the thing it links to, and as a symlink?

A symlink can links to a file of different types. For example, let mylk be a symlink which links to a dir. The following two will do different things:

mv mylk ~

mv mylk/ ~

Does whether a symbolic treated as what it links or itself depend on what it is expected to be by a command, or by how it is specified (irrespective of the command that uses the link)? Thanks.

Tim
  • 101,790

1 Answers1

3

A program that acts on the contents of a file always acts on the target, not on the symbolic link, because symbolic links have no contents of their own.

A program that acts on the metadata of a file (timestamps, owner, permissions, …) usually acts on the target, but some programs have options to act on the symbolic link instead (for example, chown -h, touch -h, …).

A program that acts on a directory entry usually acts on the symbolic link. Operations like renaming and deleting act on a directory entry, whatever it is. There are separate system calls to access the metadata (including the file type) of a file depending on whether the program wishes to follow symbolic links (stat) or not (lstat). Some programs that can act on any type of directory entry have options to tell them to act on the target instead of the link when they find a link. For example, many utilities that traverse directories recursively (find, chown -R, cp -R, …) act on symbolic links by default, but follow all symbolic links if you pass the option -L, and follow the symbolic links on the command line but not symbolic links to directories found during recursive traversal if you pass the option -H.

If a filename has a trailing slash, then this forces the filename to be interpreted as a directory. If the name is a symbolic link, it will be followed. So mv mylk ~ moves whatever mylk is (symbolic link or otherwise), while mv mylk/ ~ moves mylk if it is a directory, or the target if mylk is a symbolic link to a directory, and complains if mylk is neither a directory nor a symbolic link to one. This general behavior

  • I thought technically from an internal standpoint, a symlink is a file but with special type of file where the contents is only a directory/filename. – mdpc Oct 14 '14 at 02:05
  • @mdpc It is an adequate description of a symlink, but the contents is analogous to the content of a file, it doesn't work in the same way. The “content” is only accessible to applications by calling the special-purpose readlink system call, not by the ordinary open/read sequence. Some filesystems use the same mechanism to store file contents and symlink targets, but I think most use a different mechanism that's faster but restricted to tiny amount of data (4096 bytes for ext4). – Gilles 'SO- stop being evil' Oct 14 '14 at 07:50
  • Thanks, Gilles! I wonder what you meant by "A program that acts on a directory entry"? what kinds of operations are acting on a directory entry? – Tim Mar 22 '15 at 01:37
  • @Tim Programs that act on files as a whole, as opposed to acting on their content or metadata. For example, mv, rm, find, … – Gilles 'SO- stop being evil' Mar 22 '15 at 01:49
  • do you mean acting on the inode or filename of a file? A directory entry is a pair of inode and filename, correct? – Tim Mar 22 '15 at 01:58
  • @Tim I mean acting on the directory entry. In the case of mv, that leaves the inode intact and changes the name. In the case of find, the inode is accessed, as well as the file metadata, and the content for a directory. In the case of rm, that removes the directory entry and either removes or modifies the inode (depending on whether the file's usage count has dropped to zero, which it may not have if the file is still open or had other hard links). – Gilles 'SO- stop being evil' Mar 22 '15 at 02:06
  • "mv mylk/ ~moves the target if mylk is a symbolic link to a directory", while I ran it, and its output says mv: cannot remove ‘mylk/’: Not a directory. – Tim Mar 23 '15 at 07:02