-1

When using the ls -lSh command, the output of the size of the files is very small. For instance:

ls -lSh | grep Xcode.app #my command to terminal while in Applications directory

drwxr-xr-x@ 3 root wheel 96B Dec 17 14:59 Xcode.app #output

As you can see, Xcode is only 96B, when in reality is over 6GB on disk. Can somebody please explain the formula being applied here?

I am running iTerm2 on macosx catalina

nomadj
  • 31
  • 5

3 Answers3

2

.app are actually directories, nor regular files. Try du -sh Xcode.app instead.

aksommerville
  • 358
  • 1
  • 5
2

ls does not report size of whole directory, but only the directory entry itself. If you want aggregate directory size, you should use du or similar utilities that count sizes recursively. The size output from ls is the size of the directory entry, which generally grows if you have more files and subdirectories, but not when you have large single files, and depends a lot on the underlying filesystem.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • Can you explain (or point me to a link) in detail the discrepancy in size between using du -sh (outputs 9.0GB), and cmd + i in finder (9.62GB on disk)? – nomadj Dec 31 '19 at 19:27
  • @nomadj I am unsure about macOS, but `du` command reports **d**isk **u**sage, while File Info reports "apparent size", i.e. the size a file claims to have. Try creating a big empty file with `truncate -s 1G filename` and compare both tools on the file. – iBug Dec 31 '19 at 19:37
  • in this case ls -lh returns the alloted size of the empty file (1GB), du -sh returns used space(0B), and info returns both, with the allotted size being more precise(1.07GB). In both cases there is a 7% difference. – nomadj Dec 31 '19 at 20:05
  • @nomadj macOS File Info uses 1000-based units, while CLI utilities often use 1024-based units, so 1 GB in `ls` or `du` equals to 1,073,741,824 bytes, corresponding to the 1.073 GB shown by Finder. – iBug Jan 01 '20 at 04:45
1

The -l switch shows the "allocated size" of the item you're listing. In case of a directory, its size amounts to just its entry in the file system, and that is very small. What one normally thinks of as the "size" of a directory is actually taken by the files residing beneath it, and that is reported by the du command.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Note that a file can be in multiple directories, so considering the file to be part of the directory's size makes no sense. – Jörg W Mittag Dec 31 '19 at 19:58