Do Symbolic Links Affect Disk Usage? – Detailed Analysis

disk-usagesymlink

I've read in so many websites that, in Linux, symbolic links (soft links, symlinks) are just like pointers that reference another file, which may be located anywhere (like Windows shortcuts). However, when I check the disk usage of a folder in which there are symbolic links, there's a mismatch between what my file manager says and what du reports. However, if I type du -L (-L, --dereference; dereference all symbolic links from the man page), the output of du -L and the size that my file manager reports are the same.

My question is: if I have a softlink to a big file in, for example, my separate home partition, will I have any problems?

Example:

My /var/tmp folder is now plain empty. Let's create a file:

$ cat /some/file.txt > file.txt
$ du -ac
164 ./file.txt
168 .
168 total

And my file manager (Thunar, in this case) reports

Size: 1 item, totalling 163.0 kB

All right. Now, lets create a really big file in /tmp and a symlink to it:

$ cat /dir/really_big.txt > /tmp/heavy.txt
$ du -a | grep heavy.txt
408 ./heavy.txt
$ ln -s /tmp/heavy.txt heavy.txt
$ du -ac
164 ./file.txt
0   ./heavy.txt
168 .
168 total

Everything is fine for now. But if I open my file manager:

Size: 2 items, totalling 570.3 kB

And, finally:

$ du -acL
164 ./file.txt
408 ./heavy.txt
576 .
576 total

If the partition in which /var/tmp is located is 1 GiB big, and I create a link in it to a 1 GiB file, ¿will my hard disk die? I know that du will output 168 and Thunar 1 GiB, but I don't know which is right.

Best Answer

Symbolic links do take room, of course, but just the room it takes to store the name and target plus a few bytes for other metadata. The space taken by a symbolic link does not depend on the space taken by the target (after all, the target is not even required to exist).

Plain du reports the space taken by a directory tree on the disk. du -L reports the space that would be taken by a directory tree if all symbolic links were replaced by their target. The former is usually the useful information; for example, it's the space you'd recover if you deleted the tree, and it's (approximately) the space you need to back up the tree.

du on a directory tree shows (usually) a little more than the total of the file sizes. That's due to two things. First, du also counts directories, which take a little room to store file names and metadata. Second, du counts the disk space taken by a file, which can be different from the file size: the most common effect is that files take up an integer number of blocks (4kB on a typical Linux installation), so a 1-byte file can show as 4kB in du output; but compression (such as the primitive form provided by sparse files on just about every unix filesystem) can make the file size larger than its disk usage.

From the numbers you give, it appears that Thunar reports the sum of the sizes of the files in the directory tree, following symbolic links. It's actually saying so in a subtle way — it's claiming that the total size is 570.3 kB, not that the disk usage is 570.3 kB. What is not at all apparent from the user interface or documentation is that Thunar follows symbolic links when computing the size.

Which one is “right” is a subjective matter. du reports disk usage. Thunar reports total size following symbolic links. Creating a symbolic link has a negligible impact on disk usage, but by definition does change total-size-following-symbolic-links that Thunar reports.

Related Question