Signup/Sign In
PUBLISHED ON: FEBRUARY 16, 2023

Directory Size in Linux

When querying directory size in Linux, using ls, we would have seen the directory size almost always is 4096 bytes (4 KB). That's not the directory size, but the amount of space it takes to store the directory meta, not what it contains.

The command to be used to get memory used by a directory is du, standing for "disk usage".

Getting Directory Size with du command in Linux

The du command outputs the file size of directories, and files specified. If a directory is specified, du summarizes the memory of every subdirectory recursively and then sums it up to produce the directory space. For no path specified, the current working directory is used.

With no options, and if the specified path is a directory, du summarizes the disk usage of each subdirectory compactly, in bytes.

For example, if we run the following

du Documents/

We get an output that looks like

du  Documents/

But this would be unreadable, and very long for very deep directories. To make it compact we use the -s (summary, only directory size) flag, and the -h (human flag, like G for Gigabytes).

du -sh Documents/

But say, we want the size of every subdirectory, in a specified directory, but a total as well, we add the -c flag.

du -shc Documents/*

When we do data transfer, the size transmitted is the apparent size of the directory, and/or files. This causes du size to be of different values, when --apparent-size flag is used, or not.

Conclusion

du command is when we want to find the disk usage of a directory or file.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.