I'm trying to collate old Time Machine backups from different computers I had over the years, and want to keep only the last unique version of every file. I managed to do that using ls -d 20* | while read dir; do find "$dir" -type f -links +1 -exec rm "{}" \; done, and that worked like charm (took a while though).

The problem is that there are many linked directories, so I still have loads of duplicates because of them, and I can't find any information anywhere about how to unlink directories. For example:

iMac:ETH Mac root# stat -f "%i %N" */"HDD/Users/etolnay/.macports/opt/local/var/macports/sources/rsync.macports.org/release/ports/net/ctorrent"
21569569 2012-02-20-143022/HDD/Users/etolnay/.macports/opt/local/var/macports/sources/rsync.macports.org/release/ports/net/ctorrent
21569569 2012-03-02-054540/HDD/Users/etolnay/.macports/opt/local/var/macports/sources/rsync.macports.org/release/ports/net/ctorrent
23843405 2012-04-06-061205/HDD/Users/etolnay/.macports/opt/local/var/macports/sources/rsync.macports.org/release/ports/net/ctorrent
23843405 2012-04-13-134054/HDD/Users/etolnay/.macports/opt/local/var/macports/sources/rsync.macports.org/release/ports/net/ctorrent

There are two versions of the folder above, stored for two dates in each case. I want to delete the 2012-02-20-143022 and the 2012-04-06-061205 leaving the others. I could only do this by unlinking these directories, but unlink doesn't work on directories, and neither does gunlink from GNU CoreUtils in Homebrew, as suggested by https://superuser.com/a/557425/490956.

How can I unlink those two directories and leave only their latest entry?

  • Best way to delete older versions is from the Finder itself - but idk if that applies when it's not a backup related to that Mac. Hardlinks shouldn't be an issue… as they're just multiple references to a single file, unless you copied the TM drive using anything other than Finder.
    – Tetsujin
    Sep 22 '19 at 15:10
  • The problem @Tetsujin is that you can't delete a non-empty directory without deleting its contents, and if you delete its contents, then you're deleting the contents of the directory I want to keep, because they're hard-linked. I need to unlink a non-empty directory (which if misused can damage the file system, I understand, but should be OK if I know that the inode is also used elsewhere) Sep 22 '19 at 15:21
  • Why do you need to delete them? They're just hard links, so they don't take up any additional space on disk (except a tiny bit for the directory entry). Sep 23 '19 at 20:06

Your Answer

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

Browse other questions tagged or ask your own question.