Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding files that are *not* hard links via a shell script

Tags:

shell

hardlink

Given a directory, how do I find all files within it (and any sub-directories) that are not hard-linked files? Or more specifically, that are not hard-linked files with more than one reference?

Basically I want to scan a folder and return a list of unique files within that directory, including directories and symbolic links (not their targets). If possible, it'd be nice to also ignore hard-linked directories on file-systems that support them (such as HFS+).

like image 613
Haravikk Avatar asked Nov 27 '22 11:11

Haravikk


1 Answers

find has an option that should be useful:

find . -type f -links 1 -print

Files that are hard linked by definition have a link count of 2 or greater, so this will show all files that have no other links to them.

like image 154
twalberg Avatar answered Dec 30 '22 18:12

twalberg