Recursively find files with specific hard link count

5

1

I have a "tracking" directory containing hardlinks to files/dirs in a second directory ( used for tracking moves/renames). If I delete something in the original folder, no disk space is freed as its hardlink still exists. So I want to clean up this "tracking" directory periodically. Therefore I need to find all files in it, that have a hardlink count of 1.

What is the fastest way to find (and remove) recursively all files with a hardlink count of 1?

I know I can do something like find . -type f -exec ls -l {} \+ | grep -P "^.{11}1" and then some more piping/regexing, but this is ugly and slow. I am looking for something cleaner and faster.

imsodin

Posted 2016-08-16T22:13:09.323

Reputation: 183

2Check if your find supports -links option. – Kamil Maciorowski – 2016-08-16T22:21:50.260

1@KamilMaciorowski Thanks a lot. I stupidely only greped the manpage for "hard" and thus did not find this option. – imsodin – 2016-08-16T22:23:15.963

Answers

6

My find has -links option (I'm on Ubuntu 14.04.5 LTS). To find files that have no other hardlinks use:

find -type f -links 1

The command to remove these files is:

find -type f -links 1 -exec rm -f {} +

Kamil Maciorowski

Posted 2016-08-16T22:13:09.323

Reputation: 38 429