Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Are you sure that one program isn't reporting for example in Mib, and another in MB? –  HandyGandy Oct 9 '10 at 13:45
    
Nope, it's not a units problem. –  Juanlu001 Oct 10 '10 at 10:07

2 Answers 2

up vote 24 down vote accepted

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.

share|improve this answer
    
I've edited my question so I think is pretty clear now, but thank you for the answer. –  Juanlu001 Oct 10 '10 at 10:06
    
@Juanlu001: I've updated my answer accordingly. In short, du shows disk usage, while Thunar shows something else. –  Gilles Oct 10 '10 at 10:57
    
All right now - Thanks! :) –  Juanlu001 Oct 10 '10 at 11:58
    
The difference is important when you create backups that resolve symlinks. You may want to do that if you know you have several symlinks below a tree that resolve to locations outside a tree, but the rest of the stuff outside that tree is not important to you. –  Mel May 10 '11 at 2:32

I think by default your file manager is trying to get the size of the files that the soft links are pointing to, whereas du gives you the size of the directory and the soft links themselves but not the files they are pointing to.

To clarify,

`du`    -> size of directory + size of all the softlinks  
`du -L` -> size of directory + size of all the files that the softlinks are pointing to.

I'm not sure if this is what you were asking, but if it is, then I believe this could be the answer to your question.

share|improve this answer
    
Sorry that didn't answered my question: what you said I already figured out. I have just edited it. –  Juanlu001 Oct 10 '10 at 10:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.