Size of symlink

Sort of, but note that the size of a file is not well-defined at that level of precision.

A symbolic link involves four parts:

  • The name of the link, which is stored in the directory where it is an entry.
  • Other metadata that is present for every directory entry, to locate the rest of the metadata. This is typically the location of an inode. In addition, each directory entry costs a few more bytes, for example to pad file names and to maintain a data structure such as a balanced tree or hash.
  • Other metadata of the symbolic link itself such as timestamps. This metadata is also present for other file types (e.g. an empty regular file).
  • The target of the link.

If the filesystem allows symbolic links to have multiple hard links, the first two parts are per directory entry, the last two parts are present only once per symlink.

In ext2/ext3/ext4, the target of a symbolic link is stored in the inode if it's at most 60 bytes long. You can confirm that by asking du: it reports 0 for symlinks whose target is ≤60 bytes and one block for larger targets.

Just like for a regular file, the figure reported by du excludes the storage for the directory entry and the inode. If you want to know exactly how much space the symlink takes, you have to count those as well. Most classical filesystems allocate inodes at filesystem creation time, so the cost is split: the size of the directory entry counts against the number of blocks of data, the inode counts against the inode pool size.

For the size of the directory entry itself, the exact number of bytes taken up by an entry can depend on what other entries are present in the directory. However a directory usually takes up a whole number of blocks, so if you create more and more entries, the size of the directory remains the same, until the entries no longer fit in one block and a second block is allocated, and so on. To see exactly how the directory entries are stored in the block, you'd need a filesystem debugger and a passable understanding of the filesystem format, or a good to excellent understanding of the filesystem format as well as the knowledge of what other entries are present in the directory and possibly the order in which they were created and other entries were removed.

In summary, the “few bytes for other metadata” are:

  • The directory entry, of a variable size. Creating the symbolic link may either make no difference or add one block.
  • The inode.

And the target may occupy anywhere from 0 to one block in addition to that.