10

I have gone through a number of docs. which have lot of discrepancies over either hardlinks or softlinks taking any space in the file system.Can anyone clear this out for me?

For hardlinks i found out this:

I had a file c1 in my home directory which i hardlinked with d1 in same directory.both c1 and b1 have 11 byte size.Now when i am doing a "ls -lrt" the total bytes for all files listed(excluding d hidden files ofcourse) is 64 bytes. now when i remove hardlink d1 and again do a ls -lrt it gives me 60 bytes.does that not mean hardlinks occupy space in hard disk, but lot of docs.negate this fact, why?

I could have checked same way creating a soft link for the file and then deleting it, but since my soft link has only 2bytes size,i dont think deleting it would have any significant effect on the round off total size listed in the output of ls -lrt.

So what is with this?

0
11

Of course they occupy (a little bit of) space:

  • hard linked files have multiple directory entries. Each entry occupies space in the directory itself, but from then on, they share the same structures: inode and data area are shared. But I am not sure how this is accounted; maybe the directory entries always occupy a multiple of a certain size or so.

  • Symlinks occupy space for the directory entry as well, plus one inode which holds information about the link properties as well as the link target itself.

With that small sizes (11 bytes of data in the file) the overhead counts more than the real data. With bigger files, however, this small overhead of few bytes is negligible.

8

Yes. They both take space as they both still have directory entries.

A hardlink entry (really, a "normal entry" that [often] shares an inode) takes space, as does a symlink entry which must store the link path (the text itself) somehow. The actual space required is slightly different due to allocation/layout rules determined by the exact filesystem implementation (e.g. block/tree sizes and how the symlink is stored).

However, the amount of space is minimal and can be [almost always] considered inconsequentially in relationship to data in the files themselves.

1
  • 2
    to extend your last sentence: ... unless it is a system with many many small files and you run out of available inodes. On some file systems this is a realistic scenario and I have run into it, although not in conjunction with symbolic or hard links. Dec 1, 2011 at 9:34
7

ls is wrong!

It counts hard linked files multiple times. ls -l will just add up the block numbers of each entry, no matter how many hardlinks it has.

(Use ls -1si to show each file's inode number as well as block usage)

Try du -Ssb . instead. This will give you the correct disk usage in bytes. Unless you use the -l switch, which will reproduce ls's behaviour.

2

To be short and simple a hardlink is a reference to the inode within the filesystem. Some utilities will read this incorrectly. It will not take up any disk space.

4
  • 4
    Since when are inodes not taking up space? ... Dec 1, 2011 at 9:32
  • 3
    inodes do take up space sir, I 100% agree, but a reference to said inode is very negligible in terms of overall disk space. I hope this clears up my answer. Dec 15, 2011 at 1:02
  • This is simply wrong. The link is a piece of information that needs to be stored "somewhere". Depending on your file system this is handled slightly different but is needs to be stored. Mar 1, 2018 at 15:18
  • 2
    Bored? Responding to something almost 7 years ago, which I already clarified? Mar 8, 2018 at 21:53

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