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: find -samefile lists every hard link to the same file

Find
~/tips/tip-find-samefile-hard-links

find /srv -xdev -samefile /srv/downloads/movie.mkv

Shows all the names a file has on disk, so you know what still keeps it alive.

A hard link is a second name for the same data. Delete one name and the data stays, because the other name still points at it. That makes them easy to lose track of.

-samefile finds every name that points at the same inode as the file you give:

find /srv -xdev -samefile /srv/downloads/movie.mkv

If the downloads folder and the media library are hard linked, which is how a lot of download setups save space, this prints both paths. A hard link in another folder shows up right next to the original.

To know beforehand whether a file has other names at all, look at the link count. ls -l shows it as the number after the permissions, and anything above 1 means there are more names somewhere. find /srv -xdev -type f -links +1 lists all files that have extra names.

I’d add -xdev on purpose. Hard links can’t cross filesystems, so searching other disks is wasted time. It also means you have to start the search on the right disk: if the link lives on a filesystem you didn’t include, find won’t see it. And this is about hard links only. Symlinks to the file are a different thing, and -samefile skips them unless you add -L.

Further reading: find(1) manual page, GNU findutils manual

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find / -xdev -user olduser -ls 2>/dev/null All Find tips (46) All topics

Link