Monthly Archives: March 2012

“Downlinking” hardlinked files without unlink()ing them

Do you use hard links for organizing files but still find them a bit dangerous? Here’s a bash function that will delete files only if their link count is greater than 1.

dnl() {
  err=0
  for x in "$@" ; do
    if [[ -f "$x" && `stat -f %l "$x"` -gt 1 ]] ; then
      unlink "$x" || err=1
    else
      err=1
    fi
  done

  return $err
}

One thing: this operation is not atomic — if that is important in your use case, please do not use this.