“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() {
  for x in "$@" ; do
    if [[ -f "$x" && `stat -f %l "$x"` -gt 1 ]] ; then
      unlink "$x" || err=1

  return $err

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

This entry was posted in Everydot. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>