Skip to content
shellhacks.dev
Command Line

Make an Instant Reflink Copy on Btrfs or XFS with cp

Copy a large file instantly with cp --reflink=auto, which shares data blocks on Btrfs or XFS and falls back to a normal copy elsewhere. Needs coreutils 7.5 or later.

1 min read
Summarize in AI

You copy a multi-gigabyte file, such as a VM image, and want it to finish at once and use no extra space.

Shell
dd if=/dev/zero of=big bs=1M count=50 status=none
cp --reflink=auto big copy && cmp big copy && echo "auto: copied, contents equal"
cp --reflink=always big clone
echo "always: exit $?"
df --output=fstype . | tail -n 1
Text
auto: copied, contents equal
cp: failed to clone 'clone' from 'big': Operation not supported
always: exit 1
overlay

On a file system that supports it, --reflink creates a new file that shares the original's data blocks until either copy changes, so the work is a metadata operation. auto falls back to an ordinary copy when the file system cannot do that, while always fails instead, which is what the run shows on Docker's overlay file system: failed to clone, exit status 1. See cp invocation.

The source and the target must be on the same file system, and ext4 and overlayfs cannot do it, so this run only demonstrates the fallback. Because the blocks are shared, a reflink copy is not a backup against a failing disk.

Share

More snippets

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.