13

I'm using these commands:

du -sh --apparent-size ./*
du -sh ./*

both reporting:

4.0K    ./Lightroom_catalog_from_win_backup
432M    ./Lightroom catalog - wine_backup

while those directories contain:

$ll ./"Lightroom catalog - wine_backup"
total 432M
-rwxrwx--- 1 gigi gigi 432M Mar 18  2018 Lightroom 5 Catalog Linux.lrcat
-rwxrwx--- 1 gigi gigi  227 Nov 21  2015 zbackup.bat
$ll ./Lightroom_catalog_from_win_backup
total 396M
-rwxrwx--- 3 gigi gigi 396M Dec 17 09:35 Lightroom 5 Catalog Linux.lrcat
-rwxrwx--- 3 gigi gigi  227 Dec 17 09:35 zbackup.bat

Why du is reporting 4.0K for ./Lightroom_catalog_from_win_backup and how could I make it to report correctly?

PS: other system information:

$stat --file-system $HOME
  File: "/home/gigi"
    ID: 5b052c62a5a527bb Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 720651086  Free: 155672577  Available: 119098665
Inodes: Total: 183050240  Free: 178896289

$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:        16.04
Codename:       xenial
  • To build on the answers already given so far, what does /bin/ls -li './*/Lightroom 5 Catalog Linux.lrcat' return? – doneal24 Dec 17 '18 at 18:01
  • ls -li ./*/"Lightroom 5 Catalog Linux.lrcat" 36831321 -rwxrwx--- 1 gigi gigi 432M Mar 18 2018 ./Lightroom catalog - wine_backup/Lightroom 5 Catalog Linux.lrcat 36833201 -rwxrwx--- 3 gigi gigi 396M Dec 17 09:35 ./Lightroom_catalog_from_win_backup/Lightroom 5 Catalog Linux.lrcat – adrhc Dec 17 '18 at 18:09
  • 1
    This presents a problem with the answers since the file with the link count of three is not being counted elsewhere in the du command. So you have only two subdirectories in your working directory? – doneal24 Dec 17 '18 at 18:16
  • no, I have approximately 15 others – adrhc Dec 17 '18 at 18:29
23

I can reproduce if the files are hard links:

~ mkdir foo bar
~ dd if=/dev/urandom of=bar/file1 count=1k bs=1k
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00985276 s, 106 MB/s
~ ln bar/file1 foo/file1
~ du -sh --apparent-size foo bar
1.1M    foo
4.0K    bar

This is expected behaviour. From the GNU du docs:

If two or more hard links point to the same file, only one of the hard links is counted. The file argument order affects which links are counted, and changing the argument order may change the numbers and entries that du outputs.

If you really need repeated sizes of hard links, try the -l option:

-l
--count-links
Count the size of all files, even if they have appeared already (as a hard link).

~ du -sh --apparent-size foo bar -l
1.1M    foo
1.1M    bar
15

Notice how the link count is 3 for the two files Lightroom 5 Catalog Linux.lrcat and zbackup.bat in Lightroom_catalog_from_win_backup.

This means that these two files are hard linked to (additional names for) other files somewhere. When you run du on a directory or a set of files, each hard link is only counted once.

Example:

$ ls -l
total 41024
-rw-r--r--  2 kk  wheel  10485760 Dec 17 09:07 file1
-rw-r--r--  2 kk  wheel  10485760 Dec 17 09:07 file2

$ du -h file1
10.0M   file1

$ du -h file2
10.0M   file2

$ du -h .
10.0M   .

This behaviour is explicitly mandated by the POSIX standard for the du utility:

A file that occurs multiple times under one file operand and that has a link count greater than 1 shall be counted and written for only one entry.

Some du implementations have non-standard options to disable this behaviour. For GNU du, this is done with the -l option.

3

It's almost certainly working correctly. du counts each file only once regardless of how many times it's referenced. It's probable that your two directories contain the same set of hard-linked files.

The man page for GNU du offers -l, --count-links to switch off this standard optimisation (see man du to check if your implementation includes this). Or you run du twice, once for each directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.