4

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.

  • 2
    Check if your find supports -links option. – Kamil Maciorowski Aug 16 '16 at 22:21
  • 1
    @KamilMaciorowski Thanks a lot. I stupidely only greped the manpage for "hard" and thus did not find this option. – imsodin Aug 16 '16 at 22:23
5

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 {} +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.