brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: cp -al makes an instant copy of a folder tree that takes almost no space

Linux
~/tips/tip-linux-cp-al-hardlink-copy

cp -al photos photos-before-cleanup

Copy a big folder in seconds as hard links, as a quick snapshot before you change things.

Copying a folder of hundreds of gigabytes takes time and space. When source and copy live on the same filesystem, cp -al skips the copying entirely.

cp -al photos photos-before-cleanup

-a keeps permissions, times and the folder structure, and -l makes hard links instead of copying file contents. Every file in the new tree is the same file on disk as the original, so it is done in seconds and uses only a little space for the new folder entries. You can see it with stat -c '%h %i' photos/a.jpg: the link count goes up to 2 and both paths share one inode number.

Deleting, renaming or moving files in one tree does not touch the other. I’d call it a handy safety net before a big cleanup, where you mostly delete and reorganize. The same trick is how tools like rsnapshot keep many daily snapshots cheaply.

The catch is editing. A program that changes a file in place changes it in both trees, because there is only one file. Many editors write a new file and rename it, which breaks the link and keeps the copy safe, but databases, log writers and some image tools write in place. So this is not a backup: it protects against deletes, not against changes or a dead disk.

Hard links cannot cross filesystems, so cp -al fails with an error when the destination is on another disk.

Further reading: cp(1) manual page, link(2) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link