ls -lS Command Not Showing True Directory Size – Why

directoryls

I used ls -lS command in my home directory. This command is supposed to list down the contents of a directory by size. This is what I got after running this command

total 10148
-rw-rw-r--  1 rahul rahul 8053159 May 15 15:35 Costa_ODE.pdf
-rw-rw-r--  1 rahul rahul 1755507 May 15 17:33 gnuplot.pdf
-rw-rw-r--  1 rahul rahul  218048 May 13 22:14 out.log
-rw-rw-r--  1 rahul rahul   98131 Feb 16 01:53 hs_err_pid8639.log
-rw-rw-r--  1 rahul rahul   12364 Apr 19 14:01 Untitled 1.csv
drwxr-xr-x  4 rahul rahul   12288 Jun  6  2016 cfitsio
-rw-r--r--  1 rahul rahul    8980 Feb  7  2016 examples.desktop
drwxrwxr-x  2 rahul rahul    4096 Mar 10 12:24 bin
drwxrwxr-x  8 rahul rahul    4096 May  8 14:51 boxfitv2
drwxrwxrwx  2 rahul rahul    4096 Jan 30 11:50 dao2
drwxrwxr-x  2 rahul rahul    4096 Mar 12  2016 deja-dup
drwxr-xr-x  6 rahul rahul    4096 May 16 02:12 Desktop
drwxr-xr-x  3 rahul rahul    4096 May 15 10:53 Documents
drwxr-xr-x  5 rahul rahul    4096 May  8 14:09 Downloads
.
.
.
.

and its a pretty big list. But I want you to focus on sub-directories, for example Desktop. Its size is shown as 4096 bytes. But when I tried to see the details of Desktop, this is what I got.

Desktop-size

In short, the command ls -lS is not calculating the size of the contents of Desktop and other sub-directories. Is there any way to do it?


EDIT:

Output of ls -lsh command

total 10M
4.0K drwxrwxr-x  2 rahul rahul 4.0K Mar 10 12:24 bin
4.0K drwxrwxr-x  8 rahul rahul 4.0K May  8 14:51 boxfitv2
4.0K -rw-rw-r--  1 rahul rahul 3.2K May 13 13:28 c.c
 12K drwxr-xr-x  4 rahul rahul  12K Jun  6  2016 cfitsio
7.7M -rw-rw-r--  1 rahul rahul 7.7M May 15 15:35 Costa_ODE.pdf
4.0K drwxrwxrwx  2 rahul rahul 4.0K Jan 30 11:50 dao2
   0 -rw-rw-r--  1 rahul rahul    0 May 13 20:37 default.txt
4.0K drwxrwxr-x  2 rahul rahul 4.0K Mar 12  2016 deja-dup
4.0K drwxr-xr-x  6 rahul rahul 4.0K May 16 17:11 Desktop
4.0K drwxr-xr-x  3 rahul rahul 4.0K May 15 10:53 Documents
4.0K drwxr-xr-x  5 rahul rahul 4.0K May  8 14:09 Downloads
 12K -rw-r--r--  1 rahul rahul 8.8K Feb  7  2016 examples.desktop
.
.
.

Output of du -sh ~/Desktop command

80M /home/rahul/Desktop

Best Answer

ls -lS is indeed showing the true size of the directory: the directory itself + references to any file contained in the given directory.

You could use du instead of ls:

du -h --max-depth=1 | sort -hr

du: estimates file space usage recursively for directories

h: human readable

--max-depth=1: so you only check for the directories within the current directory

sort -hr: sorts it decreasingly

Related Question