Skip to content

 How to get the actual directory size using du?

  • by

There are different commands like du, find as well as different methods you can use to get actual directory size in a Linux/Unix system. But these commands have some advantages and disadvantages also.. Let us see how these commands can be used in the below section

The below code illustrates how you can use the Unix standard tools to get the directory size without going for commands like du or find and this is one of the good methods to find the directory size.. (This code is from here)

find ${1:-.} -type f -exec ls -lnq {} \+ | awk '
BEGIN {sum=0} # initialization for clarity and safety
function pp() {
  for(i=1;i<7;i++) {
    if(v<1024) break;
  printf("%.3f %sB\n", v, unit[i]);

And you can also use du command with apparent size parameter to get the directory size as shown below

#du command with apparent size parameter
du -hs --apparent-size

If needed you can also use a combination of du and find command to display the file sizes as shown below

#Find command with du 
find . -type f -print0 | du -scb --files0-from=- | tail -n 1

There would be some hardlinked files so to count the hardlinked files we are using the l option here. Here find command is used to pass the files to du which then counts the sizes of the files and directories it’s in.