how to get the size of a directory(total size of all files under this directory or its subdir or ...)

There is also a function in the Internal` context (for now) that does that

Internal`DirectoryByteCount[$InstallationDirectory]

(* 6794576686 *)

which is in agreement with what my OS reports for that directory

enter image description here


An estimate can be obtained without resorting to the underlying OS.

FileByteCount can be used to obtain the size of a file and FileNames can list files to any depth so combining these in to a function:

recursiveFolderSize[folder_String] := 
 Plus @@ Quiet[
   FileByteCount /@ FileNames["*", folder, Infinity] /. $Failed -> 0, 
   FileByteCount::fdir]

FileByteCount gives a message and returns $Failed for directories so the function handles these cases.

Also note that directories themselves take up disk space so the total returned will be an underestimate.