11

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
3

2 Answers 2

16

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 -ha -d 1 | sort -hr

du: estimates file space usage recursively for directories

h: human readable

a: all content, not just directory

d 1: max depth 1, so you only check for the directories within the current directory

sort -hr: sorts it decreasingly

7
  • It seems to be working only for sub-directories. The output doesn't contain the name of any other files present in the directory.
    – RD017
    May 16, 2017 at 12:11
  • 1
    Well then you can check man du and you will see there's an option for that, -a. Gonna add it to the answer though
    – M. Becerra
    May 16, 2017 at 12:12
  • That worked. thanks. But I have one small question too. Why the names of directories and files are starting with ./, for example 80M ./Desktop. Can this issue be solved?
    – RD017
    May 16, 2017 at 12:17
  • You are welcome :) That's a different question, and its not an "issue" to be solved, it just means that the file is in the current directory. See it as HERE/Desktop. If you want to reformat the output to look different, please post a new question for that
    – M. Becerra
    May 16, 2017 at 12:21
  • @RD017 this is because the du didn't specify a target (either /an/absolute/path or ./a/relative/one) so by default du is done on: ./ (ie,the current directory) May 23, 2017 at 13:32
8

ls shows the size of the regular files (or, in case of directories, the size of its inodes, not just their content, as it has no quick way to determine that, whereas for regular files the size is known and thus can be displayed exactly and quickly).

Actually that field differs depending on what the file represents :

  • for regular files : it shows their actual size
  • for symlinks (symbolic links, ln -s source dest) : the length of the symlink name (as this the content of the symlink file). (ex: the symlink /dev/fd -> /proc/self/fd : has a destination path exactly 13 caracters long ( / p r o c / s e l f / f d ), so ls -l will display "13" in the 5th column, instead of the size of the pointed-at file.)
  • for directories: the size of an inode (if the content of the directory entries fits into one) or multiple inodes (if there was a need for multiple inodes to describe the list of that directory's entries). This is why you see 4096 for most of them : usually they don't have many files inside them so it fits all into 1 inode, which is usually 4096 bytes per default. If you ever put MANY files in some directory, this will go up (and most likely will stay up afterwards, unless you recreate the directory itself).
  • for pipes, and other files types : each time the field usually associated with size may or may not be a size (ex: for block-devices (ex: /dev/hd* files ) it doesn't show any size but instead shows their major, minor pair of information. See man mknod. See man ls to see how they are identified as well.)

To know the sum of content of directories + subdirs:

  • du /some/path # will show for each directory : the sum of its content (including subdirs), and shows that for every directory at and underneath /some/path

  • du -s /some/path # will show only 1 level, ie just the total for /some/path

  • du -S /some/path # show the content of each dir, not including their subdirs. Usefull to know exactly which of the subdir of /some/path are big.

See https://linux.die.net/man/1/du for more details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .