We may wish to see how big our directories are. This can be made more difficult if those directories contain links to other files in the file system. For example, we might have a single folder that has links to all the important files for backup purposes.
If used correctly, the du command can help us find out how much space is used by our files.
In this tutorial, we’ll see how to use the du command when working with folders that have hard links.
2. The du Command
The du command shows us how much disk space our files take up.
2.1. Using du to Summarize
We can summarize the contents of a directory by using the -s switch of du.
Let’s look at an example where we have some files:
$ ls actual_files $ ls actual_files/ a_large_file another_large_file
Here, we have a folder named actual_files that has two files. Let’s use du to calculate the size of the actual_files directory:
$ du -sh * 155M actual_files
Here, we’ve used the -h switch as well as the -s to show us the sizes in human-readable values.
2.2. Default du Behavior With Links
Now, let’s create links to our files in a separate directory:
$ mkdir to_backup $ ln actual_files/a_large_file to_backup/link_to_a_large_file $ ln actual_files/another_large_file to_backup/link_to_another_large_file $ ls to_backup/ link_to_a_large_file link_to_another_large_file
So, we’ve created a new folder and created links to files in the folder named actual_files. Let’s see how du handles it:
$ du -sh * 155M actual_files 4.0K to_backup
Isn’t that surprising? The original folder is over 150 MB, but the folder with the links is just 4 KB, even though it links to the same files.
However, if we try to compute the size of just the folder with the hard links, it produces the desired result:
$ du -sh to_backup/ 155M to_backup/ $ du -sh actual_files/ 155M actual_files/
The reason why it behaves differently is a little involved, and we’ll look at it later. Let’s first look at how to make the du command produce the output we want.
3. Counting Links With the du Command
When working with links, the du command has several useful flags. Of specific interest to us here are the -L and the -l flags. The first of these is for symbolic links. Let’s use the second, which helps us with hard links:
$ du -slh * 155M actual_files 155M to_backup
As we can see, we now have the exact result we’re looking for.
4. Hard Links and the du Command
To understand why the du command behaves the way it does, let’s look at some Linux internals.
4.1. Inodes and Hard Links
In Linux, a file is internally represented by an Inode. An Inode holds the file’s metadata. It’s represented by a unique number. When we create a hard link to a file, we are just creating another reference to the same Inode.
Once a hard link is created, there’s no difference between the hard link and the original file.
4.2. Inodes and the du Command
In the examples above, when we tried to calculate the total size of the folder without the -l flag, the du command knew that it was operating on the same Inode. So, it calculated the size just once, even though there were two references to that Inode.
When using the -l flag, we explicitly ask it to report the actual size, even for hard links. So, when it encounters a hard link to a file, it calculates the size of both the link and the file.
In this article, we saw how to use the du command when working with files and hard links.
We also looked at the underlying Inode implementation to understand why the command behaves the way it does.