Command line

Copying a Sparse File, Measured: Which Tools Preserve the Holes

The same 1 GiB sparse file — 3 MiB of real data, the rest holes — copies in 0.004 seconds with plain cp and stays 3 MiB on disk, or takes 1.7 seconds with plain rsync and arrives as a full 1 GiB. The fastest tool is also the one that preserves the holes; the slowest is the one that fills them.

Written November 10, 2026 · Tested with rsync 3.2.7 · 12 min read

By the end of this guide
  • The table: plain cp is fastest and preserves the holes; plain rsync is slowest and fills them
  • Why cp never reads the holes
  • dd conv=sparse: the honest middle
  • cp --sparse=always ties plain cp, so when does the flag matter

What this guide is

One file, eight arms, three reps each. The file is 1 GiB apparent and 3 MiB real: three 1 MiB blocks of random data written at offsets 0, 512 MiB, and 1 GiB minus 1 MiB, with nothing but holes between them. Every arm copies the same file, and every copy is checked two ways: cmp confirms the logical contents are byte-identical to the source, and du -B1 records how many bytes the copy actually occupies on disk. A copy can pass the first check and fail the second — that is the whole guide.

The question is the one people ask after copying a disk image and watching the destination disk fill up: the file was 3 MiB on the source, so why is the copy 1 GiB. The answer is that a sparse file has two sizes — the apparent size ls -l prints, and the allocated size du prints — and most copy tools only preserve the first. The table below puts numbers on all eight arms, on two separate nights, and the two nights agree in every place that matters.

The table: plain cp is fastest and preserves the holes; plain rsync is slowest and fills them

Wall seconds, medians of three runs, fresh tonight. Allocated bytes are identical on both nights for every arm.

  • cat src > dst: fresh median 0.3815, reps 0.7378, 0.3815, 0.3265. Night before: 0.3424, reps 0.7293, 0.3424, 0.3239. Allocated: 1,073,741,824 (1 GiB) — the holes are filled.
  • cp src dst (default --sparse=auto): fresh median 0.0042, reps 0.0448, 0.0042, 0.0038. Night before: 0.0040, reps 0.0452, 0.0040, 0.0035. Allocated: 3,145,728 (3 MiB) — the holes survive.
  • cp --sparse=always src dst: fresh median 0.0043, reps 0.0041, 0.0043, 0.0049. Night before: 0.0040, reps 0.0040, 0.0039, 0.0041. Allocated: 3,145,728 (3 MiB).
  • cp --sparse=never src dst: fresh median 0.5019, reps 0.6055, 0.3149, 0.5019. Night before: 0.4417, reps 0.5424, 0.4417, 0.3160. Allocated: 1,073,741,824 (1 GiB) — the holes are filled.
  • dd if=src of=dst bs=1M: fresh median 0.3472, reps 0.3472, 0.3428, 0.5405. Night before: 0.3635, reps 0.5574, 0.3442, 0.3635. Allocated: 1,073,741,824 (1 GiB) — the holes are filled.
  • dd if=src of=dst bs=1M conv=sparse: fresh median 0.0520, reps 0.0540, 0.0520, 0.0506. Night before: 0.0524, reps 0.0528, 0.0517, 0.0524. Allocated: 3,145,728 (3 MiB).
  • rsync -a src dst: fresh median 1.7098, reps 1.8039, 1.5109, 1.7098. Night before: 2.0891, reps 2.8338, 1.5767, 2.0891. Allocated: 1,073,741,824 (1 GiB) — the holes are filled.
  • rsync -a --sparse src dst: fresh median 1.1892, reps 1.1944, 1.1879, 1.1892. Night before: 1.2130, reps 1.2167, 1.2130, 1.2107. Allocated: 3,145,728 (3 MiB).

Plain cp is 91 times faster than cat (0.0042 against 0.3815), 120 times faster than cp --sparse=never (0.0042 against 0.5019), and 407 times faster than plain rsync (0.0042 against 1.7098) — and it is the only fast arm that also keeps the file sparse. The allocation column is the portable claim: every preserving arm lands on exactly 3,145,728 bytes on both nights, every expanding arm on exactly 1,073,741,824, while the wall times are tmpfs-only and will differ on real disks. The exact ratios are one machine's; the split between the two columns is not.

Why cp never reads the holes

The manual says cp by default detects holes in the source via a crude heuristic and makes the output sparse too, and --sparse=always creates a sparse destination whenever the source contains a long enough run of zero bytes, even if the input does not appear sparse. The mechanism is visible: cp --debug on the sparse source prints sparse detection: SEEK_HOLE, meaning cp asks the filesystem where the data is and skips the rest, instead of reading a gigabyte of zeros and writing them back out.

That explains both halves of the cp result. It is fastest because it moves 3 MiB, not 1 GiB — the holes are never read, never written, and never cross the page cache as data. And it preserves the holes for the same reason: a hole it never reads is a hole it never writes. On a sparse source the same command reports SEEK_HOLE, and the allocation column shows what that buys: 3 MiB out for a 1 GiB file.

Two cells deserve a sentence each, not a footnote. Plain cp spiked on its first rep on both nights — 0.0448 seconds tonight, 0.0452 the night before, about 11 times its own median — while cp --sparse=always shows no such spike on either night (0.0041 to 0.0049 tonight, 0.0039 to 0.0041 the night before). Nothing in the probe explains the spike; it reproduces almost exactly, which is why the guide prints ranges, never medians alone. And plain rsync moved between nights — median 1.7098 tonight against 2.0891 the night before, with overlapping ranges — while rsync --sparse barely moved at all (1.1892 against 1.2130). The direction is stable on both nights; the exact gap is not.

The rsync trap: -a does not mean sparse

Trap
rsync -a is the slowest arm in the table and the one that silently expands the file: 1.7098 seconds at the median, and the copy occupies the full 1 GiB. Nothing in the output says so — the copy is byte-identical to the source, cmp passes, and only du tells the story. The archive flag preserves permissions, times, and ownership; sparseness is not among them. The manual is explicit about what fixes it: --sparse tries to handle sparse files efficiently so they take up less space on the destination, and when --whole-file is in effect, as it is for a local copy, it will always work.

But the fix costs time. rsync --sparse is still 283 times slower than plain cp (1.1892 against 0.0042) and 23 times slower than dd conv=sparse (1.1892 against 0.0520), while producing the same 3 MiB allocation both do. The flag buys the allocation back — the copy lands on 3,145,728 bytes on both nights — and nothing else. For a local copy of a sparse file, rsync is the wrong tool in both directions: slow and, without the flag, wrong on disk. Its home is the sibling guide's question, copying a changed file over a network a second time, where the delta algorithm earns its keep; on a first local copy there is no delta to send, and the table shows what that costs.

dd conv=sparse: the honest middle

dd without the flag is a byte pump: it reads the holes as zeros and writes zeros, so the copy costs 0.3472 seconds and occupies the full 1 GiB. With conv=sparse the manual says dd tries to seek rather than write all-zero output blocks, and the table shows the result: 0.0520 seconds, 3 MiB allocated, the holes preserved. That is 6.7 times faster than plain dd on the same file, and 12 times slower than plain cp, which never touches the holes at all — dd still reads them to discover they are zeros, cp skips them by asking the filesystem first.

The conv=sparse arm is the honest middle of the table: it preserves the holes, it costs a real but small amount of time, and its numbers barely move between nights (0.0520 tonight, 0.0524 the night before, ranges 0.0506 to 0.0540 against 0.0517 to 0.0528). If the tool must be dd — a raw device, a block count, a pipeline that already speaks dd — the flag is the difference between a 3 MiB copy and a 1 GiB one, and there is no reason to run without it on a file that might be sparse.

cp --sparse=always ties plain cp, so when does the flag matter

On this file the flag buys nothing measurable: cp --sparse=always at 0.0043 against plain cp at 0.0042, the same 3 MiB allocation, the same cmp pass. The file is already sparse — its holes are real unallocated extents — so the default --sparse=auto detects them and does the right thing. The flag matters for the file this probe did not run: a dense file whose zeros were written explicitly, block by block, so the filesystem holds real zero blocks instead of holes. There the default has nothing to detect, and --sparse=always rebuilds the file sparse by turning long zero runs back into holes. That is the manual's wording, not a measurement here — no dense-zero arm ran — and the guide states it as such. The same flag is the repair after a careless copy: run it on the bloated 1 GiB copy and the result is a 3 MiB file again, which is the recovery the Bash-Hacks piece describes.

How to check whether your copy preserved the holes

Before trusting a copy of a disk image, a database file, or a core dump, compare the two sizes. ls -l prints the apparent size — the offset of the last byte, 1,073,741,824 here on every copy, sparse or not. du -B1 prints the allocated bytes — 3,145,728 on a preserved copy, 1,073,741,824 on an expanded one. ls -ls puts both numbers side by side: the first column is the allocated blocks, the second is the apparent size. If the two disagree by an order of magnitude, the file has holes, and the copy either kept them or filled them. cp --debug shows the mechanism before the copy runs: on a sparse source it prints sparse detection: SEEK_HOLE, on a dense file it prints sparse detection: no — the same command, two different answers, and the allocation column follows the answer.

Checklist

  • Before copying a disk image, check the two sizes with ls -l and du -B1 — if they disagree by an order of magnitude, the file has holes and the copy tool matters.
  • Before reaching for rsync on a local copy, check the table: plain rsync -a is the slowest arm and expands the file to the full 1 GiB; it needs --sparse to preserve the holes, and even then it costs 283 times plain cp.
  • Before assuming cat or dd preserved anything, check the allocation: both fill the holes by default, and the copy passes cmp either way — only du tells the story.
  • Before running dd on a file that might be sparse, add conv=sparse: it preserves the holes at 0.0520 seconds against 0.3472 without it, and there is no reason to run without it.
  • Before typing --sparse=always, check whether the file is already sparse: on a sparse source it ties plain cp exactly, and its job is the dense file full of written zeros, which this probe did not measure.
  • Before quoting a ratio, check the range: plain cp spiked to 0.0448 seconds on its first rep on both nights, and plain rsync moved from 2.0891 to 1.7098 between nights, so the medians are not the whole story.
  • Before reusing these numbers, check the setup: WSL2, 32 CPUs, GNU coreutils 9.4, rsync 3.2.7, tmpfs, warm page cache, one thread per arm — the allocation split held exactly on both nights, the absolute seconds are this box's.

Sources

  • GNU Coreutils, cp invocation — read 2026-11-10: --sparse=WHEN controls creation of sparse files; auto detects holes in the source via a crude heuristic and makes the output sparse too; always creates a sparse destination whenever the source contains a long enough sequence of zero bytes, even if the input does not appear sparse; never inhibits creation of sparse files. Box runs coreutils 9.4, the manual text is 9.12, and every cited behavior was exercised by the probe itself; cp --debug on the sparse source reports sparse detection: SEEK_HOLE.
  • GNU Coreutils, dd invocation — read 2026-11-10: conv=sparse tries to seek rather than write all-zero output blocks. Box runs coreutils 9.4, the manual text is 9.12; the probe exercised it on the same sparse corpus.
  • rsync manual — read 2026-11-10: --sparse (-S) tries to handle sparse files efficiently so they take up less space on the destination; if --whole-file is in effect, as it is for a local copy, it will always work. Box runs rsync 3.2.7, protocol 31; the probe exercised it on the same sparse corpus.
  • Bash-Hacks, Find out how much disk a file really uses, not how big it claims to be — read 2026-11-10: explains apparent size (st_size) against allocated size (st_blocks); states that copying can quietly fill the holes, that GNU cp preserves them by default (--sparse=auto), that rsync needs -S, and that cp --sparse=always rebuilds a sparse version after a careless copy. No timing table, no multi-tool head-to-head, no reps — the gap this probe fills.
  • copyprogramming, The Most Efficient Way to Copy Files in Linux — read 2026-11-10: asserts cp --sparse=auto preserves sparseness, with a performance table for plain copying (cp --reflink=auto, dd optimized) that states no method, reps, or filesystem, and no sparse corpus, no dd conv=sparse arm, and no rsync --sparse arm. This guide's table is the same question on one shared sparse corpus, three reps per arm, with allocated bytes per arm.
  • Veldica, dd vs cp: File Copy Block Size, Measured — the sibling guide: the same tmpfs and warm-cache harness, different question — block size on a dense file. It explicitly defers sparse files ("Small files, many files, sparse files, and parallel copies are different questions"), and its cp --debug line ("sparse detection: no") was measured on a dense file; this guide asks the deferred question.
  • Veldica, rsync delta vs whole-file, Measured — the sibling guide: measures delta versus whole-file copying on dense data; it never measures --sparse. This guide asks the sparse question on first copy, not on a repeat copy.

Keep Exploring