Zum Inhalt

High-Integrity File Operations: Reflinks (CoW), Atomic Replacements, rsync, find & tar Pipelines

Executive Summary: Naive file manipulation in production—such as copying multi-gigabyte VM images without Copy-on-Write (--reflink=auto), overwriting live configuration files non-atomically, or running unquoted find ... | xargs on filenames containing spaces—causes data corruption and I/O stalls. This engineering reference details CoW reflink cloning, atomic mv rename semantics, rsync delta-transfer algorithms, null-delimited find -print0 / fd pipelines, and tar + zstd streaming archives.

  • Zero-Cost CoW Reflinks (cp --reflink)


    Clone 100 GiB virtual machine disks or database directories instantaneously on Btrfs, XFS, and ZFS by sharing data extents (FICLONE ioctl).

  • POSIX Atomic Rename (mv)


    Update live configuration files and symlinks with zero partial-read race conditions using the atomic rename(2) system call.

  • Delta Synchronization (rsync)


    Replicate directory trees, ACLs, extended attributes (-aAXH), and sparse files efficiently over SSH using rolling-checksum delta transfers.

  • Multi-Threaded tar + zstd


    Replace single-threaded gzip bottlenecks with multi-core Zstandard (tar -I 'zstd -T0 -6') and preserve SELinux/xattr metadata.


Understanding what happens at the kernel VFS and storage extent layer during cp, mv, and ln prevents unnecessary NVMe wear and production race conditions:

%%{init: {'flowchart': {'wrappingWidth': 600}}}%%
flowchart TD
    InodeOrig["Inode #101 (big.qcow2)"]
    InodeClone["Inode #102 (clone.qcow2)<br/>Created in < 1ms (0 Bytes Written)"]
    SharedExtent[("Shared Physical NVMe Extents<br/>Only Modified Blocks Split Later")]

    InodeOrig --> SharedExtent
    InodeClone --> SharedExtent

Atomic Configuration Update Pattern (rename(2))

%%{init: {'flowchart': {'wrappingWidth': 600}}}%%
flowchart TD
    TmpFile["1. Write New File<br/>nginx.conf.tmp (Inode #205)"]
    SyncDisk["2. fsync() Data to Disk"]
    RenameSyscall["3. mv nginx.conf.tmp nginx.conf<br/>Atomic rename(2) dentry swap"]

    TmpFile --> SyncDisk
    SyncDisk --> RenameSyscall

Why You Should Never Write Directly Over a Live File (cp new.so /usr/lib/live.so)

If a running process has /usr/lib/live.so memory-mapped (mmap) and you overwrite the existing Inode in-place (cp or cat >), the kernel modifies the underlying pages while the process is executing instructions from them, immediately crashing the process with SIGBUS (Bus Error). Conversely, if you write to a temporary file in the same directory (live.so.tmp) and execute mv -f live.so.tmp live.so, the rename(2) syscall atomically points the directory entry to the new Inode while existing processes safely continue reading the old unlinked Inode until they restart.


2. Production cp, mv, ln, and install Idioms

Use install (from GNU coreutils) instead of chaining mkdir -p + cp + chown + chmod when provisioning configuration files or binaries:

Production File Creation, CoW Cloning & Atomic Symlink Swaps
# 1. Provision a directory with strict 0750 permissions and specific owner/group in one atomic command
sudo install -d -m 0750 -o www-data -g www-data /srv/webapp/uploads

# 2. Copy a configuration file while setting 0640 permissions and validating syntax first
sudo install -m 0640 -o root -g postgres pg_hba.conf.new /etc/postgresql/17/main/pg_hba.conf

# 3. Clone a large VM image or directory using Copy-on-Write reflinks and preserving sparse holes
cp --reflink=auto --sparse=always -a /var/lib/libvirt/images/debian12.qcow2 \
                                     /var/lib/libvirt/images/k8s-worker-01.qcow2

# 4. Atomically switch a zero-downtime deployment symlink (/srv/current -> /srv/releases/v2)
#    Note: -T (--no-target-directory) + -f + -n is mandatory to replace a symlink to a directory atomically!
ln -sfnT /srv/releases/2026-11-06 /srv/current.tmp && mv -Tf /srv/current.tmp /srv/current

3. High-Precision File Discovery: find and fd

When automating cleanups or permission audits with find, never pipe newline-separated filenames into bare xargs (find . | xargs rm)—a filename containing a space or newline will cause xargs to delete unintended files. Always use -exec ... + (which packs maximum arguments into execve() safely) or null-byte delimiters (-print0 | xargs -0):

Hardened Production find & fd Queries
# 1. Find all files > 100 MiB modified in the last 7 days, excluding /proc, /sys, and /run
sudo find / -xdev -type f -size +100M -mtime -7 -exec ls -lh {} +

# 2. Safely fix permissions on all directories (0755) and files (0644) using batched -exec +
find /var/www/html -type d -exec chmod 0755 {} +
find /var/www/html -type f -exec chmod 0644 {} +

# 3. Audit the system for dangerous world-writable files or SUID/SGID binaries
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) -exec ls -ld {} +

# 4. Safe log rotation cleanup: preview with -print first, then delete files older than 30 days
find /var/log/myapp -type f -name "*.log.gz" -mtime +30 -delete

# 5. Modern Rust-based 'fd' alternative (ignores .git/hidden by default, uses regex & parallel threads)
fdfind --type f --extension conf --exec wc -l

4. rsync Delta Replication & tar + zstd Archiving

When migrating servers or synchronizing directories, standard rsync -avz drops POSIX ACLs (-A), Extended Attributes / SELinux labels (-X), and Hard Links (-H), and corrupts Sparse VM files unless -S is specified.

Replicating Directories with ACLs, xattrs, Hardlinks & Sparse Files
# Dry-run (-n) preview with itemized change log (-i) before executing live sync:
# Note: Trailing slash on source (/srv/data/) copies contents INTO /mnt/backup/data/
sudo rsync -aAXHSv --numeric-ids --delete --info=progress2 --dry-run \
  /srv/data/ root@backup-node.lan.nejat-hakan.de:/mnt/backup/data/
Creating & Streaming High-Speed tar.zst Archives
# 1. Create a compressed archive using all CPU cores (-T0) at zstd level 6, preserving xattrs/ACLs
sudo tar --xattrs --acls -I 'zstd -T0 -6' -cpf /tmp/etc-backup-$(date +%F).tar.zst /etc

# 2. Inspect archive table of contents without extracting
tar -tvf /tmp/etc-backup-$(date +%F).tar.zst

# 3. Stream a directory tree directly across SSH without creating a temporary archive file on disk
sudo tar --xattrs --acls -I 'zstd -T0 -3' -cpf - /var/vmail \
  | ssh root@new-mail-srv "tar --xattrs --acls -I 'zstd -d' -xpf - -C /"

The rsync Trailing Slash Rule

  • rsync -a /src/dir/ /dst/dir/ (WITH trailing slash on source): Synchronizes the contents of /src/dir directly inside /dst/dir/.
  • rsync -a /src/dir /dst/dir/ (WITHOUT trailing slash on source): Creates a nested subdirectory /dst/dir/dir/. Always run with --dry-run -i (-ni) first when combining with --delete!