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
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
We get an output that looks like
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).
But say, we want the size of every subdirectory, in a specified directory, but a total as well, we add the
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.
du command is when we want to find the disk usage of a directory or file.