6

While ls shows most directories having a size of 4.0K, some directories have different sizes, as shown in the output below:

drwxr-xr-x 8 root root 4.0K Aug 13  2022 room
drwxr-xr-x 2 root root  138 Jan 11  2021 room_old
drwxr-xr-x 2 root root 4.0K Aug 12  2022 room_new

Why does ls show different size for some directories (not 4k)?

randall
  • 71

1 Answers1

6

It's not really the size of the contents of the directory, but it's rather how much space on disk is used to store the meta data(e.g. about it and it's immediate contents inode numbers and names/paths) of that directory.

To simplify it, think of a directory similar to a Telephone directory ... i.e. the more names, numbers and addresses it contains, the larger in size it becomes and vice versa.

Please see the demonstration below:

# Block size
$ stat -fc %s .
4096
# Create empty directories
$ mkdir 1 2 3
$ ls -lh
total 12K
drwxrwxr-x 2 ubuntu ubuntu 4.0K Mar  8 11:43 1
drwxrwxr-x 2 ubuntu ubuntu 4.0K Mar  8 11:43 2
drwxrwxr-x 2 ubuntu ubuntu 4.0K Mar  8 11:43 3
# Create multiple empty 0 size files
$ touch 1/file{1..1000}
$ touch 2/file{1..100000}
# Create a 1G size file
$ fallocate -l 1G 3/file1
$ ls -lh
total 2.7M
drwxrwxr-x 2 ubuntu ubuntu  28K Mar  8 11:44 1
drwxrwxr-x 2 ubuntu ubuntu 2.6M Mar  8 11:44 2
drwxrwxr-x 2 ubuntu ubuntu 4.0K Mar  8 11:44 3
$ du -sh *
28K     1
2.6M    2
1.1G    3
# Copy a more populated directory with larger meta-info size under another less populated directory
$ cp -r 2/ 3/
$ ls -lh
total 2.7M
drwxrwxr-x 2 ubuntu ubuntu  28K Mar  8 11:44 1
drwxrwxr-x 2 ubuntu ubuntu 2.6M Mar  8 11:44 2
drwxrwxr-x 2 ubuntu ubuntu 4.0K Mar  8 11:44 3
$ ls -lh 3/
total 1.1G
drwxrwxr-x 2 ubuntu ubuntu 2.6M Mar  8 11:44 2
-rw-rw-r-- 1 ubuntu ubuntu 1.0G Mar  8 11:44 file1

Notice that the minimum allocatable size for directory metadata/information depends on the file system ... e.g. on Ubuntu EXT4, it defaults to the block size(find it with e.g. stat -fc %s .) ... Other filesystems, however, might utilize less/more than that ... e.g. on NTFS:

# Block size
$ stat -fc %s .
4096
# Create an empty directory
$ mkdir dir_on_ntfs
$ ls -lh
total 0
drwxrwxrwx 1 ubuntu ubuntu 0 Mar  8 13:32 dir_on_ntfs
# Populate the directory with multiple empty 0 size files
$ touch  dir_on_ntfs/file{1..10000}
$ ls -lh
total 2.0M
drwxrwxrwx 1 ubuntu ubuntu 2.0M Mar  8 13:32 dir_on_ntfs

And on XFS:

# Block size
$ stat -fc %s .
4096
# Create an empty directory
$ mkdir dir_on_xfs
$ ls -lh
total 0
drwxr-xr-x 2 ubuntu ubuntu 6 Mar  8 13:32 dir_on_xfs
# Populate the directory with multiple empty 0 size files
$ touch dir_on_xfs/file{1..10000}
$ ls -lh
total 376K
drwxr-xr-x 2 ubuntu ubuntu 240K Mar  8 13:32 dir_on_xfs

While on VFAT:

# Block size
$ stat -fc %s .
32768
# Create an empty directory
$ mkdir dir_on_vfat
$ ls -lh
total 32K
drwxr-xr-x 2 ubuntu ubuntu 32K Mar  8 13:32 dir_on_vfat
# Populate the directory with multiple empty 0 size files
$ touch  dir_on_vfat/file{1..10000}
$ ls -lh
total 640K
drwxr-xr-x 2 ubuntu ubuntu 640K Mar  8 13:32 dir_on_vfat
Raffa
  • 32,237
  • 1
    does it mean directory size always big than 4k? why a 138 byte size shows ? – randall Mar 08 '23 at 09:20
  • 1
    randall That could be the reason why @muru asked in this comment i.e. that different directory could be a mountpoint of or mounted from another FS ... I also updated the answer for that. – Raffa Mar 08 '23 at 09:36
  • I checked the format , it 's xfs, and they are on the same disk, wired – randall Mar 08 '23 at 12:31
  • they are on the same disk, does it copy from other fielsystem can keep old file meta info? – randall Mar 08 '23 at 12:35
  • I run the command stat below, it shows: File: ‘room_old’ Size: 138 Blocks: 0 IO Block: 4096 directory – randall Mar 08 '23 at 12:44
  • @randall "does it copy from other fielsystem can keep old file meta info?" ... No, copying should result in the directory recreation in the destination FS following its rules. – Raffa Mar 08 '23 at 13:03
  • @randall They could be on the same disk but different partitions/filesystems ... A directory can also be a mount point for another filesystem ... You can check if the case is the latter one with e.g. mount | grep -i "room_old" ... I can't do your homework :-) – Raffa Mar 08 '23 at 13:25
  • thanks a lot , i found a similar question here https://superuser.com/questions/585844/why-directories-size-are-different-in-ls-l-output-on-xfs-file-system – randall Mar 09 '23 at 02:46