How to duplicate a file without copying its data with btrfs

btrfscpdeduplicationfile-copy

I have no experience with btrfs, but it's advertised to be able to
de-duplicate files.

In my application, I'd need to duplicate whole directory trees.

From what I learned, btrfs only de-duplicates in some post scan, not
immediately. Even just using cp doesn't seem to trigger any
de-duplication (at least, df shows an increased disk usage in the
size of the copied files).

Can I avoid moving data around altogether and tell btrfs directly to
duplicate a file at another location, essentially just cloning its
metadata?

In essence, similar to a hardlink, but with independent metadata
(permissions, mod. times, …).

Best Answer

There are two options:

  1. cp --reflink=always
  2. cp --reflink=auto

The second is almost always preferable to the first. Using auto means it will fallback to doing a true copy if the file system doesn't support reflinking (for instance, ext4 or copying to an NFS share). With the first option, I'm pretty sure it will outright fail and stop copying.

If you are using this as part of a script that needs to be robust in the face of non-ideal conditions, auto will serve your better.

Related Question