3

I would like to know, how much is a sparse file really using on the disk.

ls, statand similar commands all seem showing its virtual size, i.e. with the holes.

I want to know the real disk usage of the file, without the holes.

How can I do that?

1

1 Answer 1

6

stat’s “Blocks” field shows the allocated blocks, which will be smaller than the apparent size with sparse files:

$ truncate -s 1g test.1g
$ stat test.1g
  File: test.1g
  Size: 1073741824  Blocks: 0          IO Block: 4096   regular file
…

du and ls can also be used:

$ ls -s test.1g
0 test.1g
$ du test.1g
0   test.1g

If you’re not seeing this kind of results, then your file isn’t really sparse, or its sparseness is hidden by a layer of indirection (some network file systems or FUSE file systems for example).

5
  • Nitpick on the last point: It’s technically possible if dealing with networked storage (and possibly with some FUSE setups) to see a file as not being sparse even though it actually is on the underlying filesystem that is actually storing it, and most people I know would still consider that a sparse file. This usually won’t matter for users though unless they’re dealing with storage quotas that care about actual usage. Commented Apr 12 at 13:06
  • It is worth noting that if your file is large enough (1G definitely is), then the number of blocks should be LARGER than the size of the file if it isn't sparse. For instance, on my ext4 filesystem, a 1G file has 77824 extra bytes allocated. Commented Apr 12 at 17:22
  • @AustinHemmelgarn fair point, although as far as I’m aware, most commonly-used network file systems do correctly report block usage with underlying sparse files, and some (NFS 4.2 for example) even optimise network transfers and avoid transferring holes. Commented Apr 13 at 15:43
  • 1
    @StephenKitt NFS and 9P definitely support them, for SMB it’s technically optional but widely implemented. The common case though where this comes up is dealing with non-filesystem file transfer protocols exposed as filesystems. Think stuff like SSHFS, davfs, or using rclone to expose an FTP server as a local filesystem. Commented Apr 13 at 18:59
  • @AustinHemmelgarn right, scenarios where sparseness can simply not be represented in the underlying protocol. Commented Apr 13 at 21:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.