0

I wanted to check what is the size of pg_backup folder, so I used this command:

[postgres@server02 ~]$ pwd
/var/lib/pgsql

[postgres@server02 ~]$ ls -lh total 4.0K drwxr-x---. 7 postgres postgres 86 Oct 6 22:00 pg_backup

As seen in the above output, the size is 86 bytes.

Hovewer, pg_backup directory itself contains several other directories:

[postgres@server02 ~]$ ls -lh pg_backup
total 0
drwxr-xr-x. 3 postgres postgres 121 Oct  2 23:00 20211002
drwxr-xr-x. 2 postgres postgres 109 Oct  3 22:00 20211003
drwxr-xr-x. 2 postgres postgres 109 Oct  4 22:00 20211004
drwxr-xr-x. 2 postgres postgres 109 Oct  5 22:00 20211005
drwxr-xr-x. 2 postgres postgres 109 Oct  6 22:00 20211006
[postgres@server02 ~]$

And then one of these sub-directories contains some big files:

[postgres@server02 ~]$ ls -lh pg_backup/20211006
total 23G
-rw-r--r--. 1 postgres postgres  23G Oct  6 23:21 file.dump
-rw-r--r--. 1 postgres postgres 418K Oct  6 22:00 backup_dba.dump
-rw-r--r--. 1 postgres postgres 1.4K Oct  6 22:00 backup_postgres.dump
-rw-r--r--. 1 postgres postgres  830 Oct  6 22:00 backup_dwh.dump

I confused why the ls -lh command shows the size of pg_backup as only 86 bytes if in reality this directory CONTAINS a number of larger sub-directories which in turn CONTAIN files that might reach 23GB in size? Why the total sum of the files in all sub-directories of pg_backup is not reflected in the initial ls -lh command?

Kusalananda
  • 333,661
kamokoba
  • 163
  • 1
  • 10
  • 3
    A directory is not the sum of its children, it is basically a table of references to location of its children. – Zoredache Oct 07 '21 at 08:06
  • @Zoredache what is the correct way to determine the real size of a directory? – kamokoba Oct 07 '21 at 08:20
  • 1
    Depends on what you mean by 'size of a directory'. If you want the not entirely exact, but rough approximation of how much storage the things contained within a directory is using then use the du command. – Zoredache Oct 07 '21 at 08:22
  • And https://unix.stackexchange.com/questions/185764/how-do-i-get-the-size-of-a-directory-on-the-command-line – muru Oct 08 '21 at 10:13

1 Answers1

5

ls shows the size consumed by a file. If we'll simplify things, a lot of things in Unix-like systems look like files. Some are file files, some are socket files, some are symlinks files, some are directory files, etc. The size shown in ls output is a size consumed by this object. But it does not take into account the size of the object the target is referring.

You are looking for

du -h -d 0 <directory>

command. And the size the ls is showing you - well, it's just not it. It will become bigger, yeah, as the directory will comprise more children, but these two sizes measure different things.

ilkkachu
  • 138,973
drookie
  • 177
  • 1
    "ls shows the size consumed by a file." - not necessarily. It shows the apparent size of a file, which isn't always the same as the actual bytes on the disk – Chris Davies Oct 07 '21 at 18:26