9

Is there any way to create a hard link in the finder?

A hard link in terminal would be created with:

ln /path/to/file /path/to/newpath

whereas a symlink / alias can be done by holding command and option or

ln -s /path/to/file /path/to/newpath

(though apparently the behaviors of 'alias' and 'ln' are different)

5 Answers 5

7

You could create an Automator service like this:

enter image description here

This takes the selected files and creates hard links to them in the directory where they exist. You can then drag and drop the links to the location where you want them.

This will work for files with spaces in their names, but not for files containing quotation marks in their name.

You can access it from the Finder » Services menu, the context menu, and you can assign it a keyboard shortcut analogous to the L shortcut for aliases (perhaps L).

5
  • 1
    Is there a way to make an action that takes effect when doing a drag/drop from one folder to another while holding down something like the option key which provides the destination folder so the hard link could be created directly in the new directory?
    – Michael
    Aug 3, 2014 at 0:29
  • @Michael that would probably do best as a question of its own and not a comment on this answer.
    – Daniel
    Aug 5, 2014 at 1:46
  • 1
    From a bash perspective that should work on files with quotation marks in the name. Is there some Automator-related reason why it doesn't? (Did you test that it actually doesn't work with quotation marks?)
    – Wildcard
    Oct 3, 2016 at 14:42
  • Related, from the Unix & Linux Stack Exchange: How to use arguments like $1 $2 … in a for loop? (There are multiple simpler syntaxes you can use.)
    – Wildcard
    Oct 3, 2016 at 14:44
  • @Daniel: I extended your solution with a refined code in order to get "My File Hardlink.txt" instead of "My File.txt Hardlink" which hampers file type association. Please peer review it Thanks.
    – porg
    Nov 10, 2016 at 14:46
2

There's no way to do this in Finder.app itself. However, if your goal is just to avoid typing the commands into Terminal.app then you could use Automator.app to create a workflow to do the commands for you.

2

I actually wanted the link term in the rename with the extension preserved otherwise finder wouldn't recognize the filetype.

so I used Daniel's Answer(thanks for that) and changed it a bit

for f in "$@"
 do  
  dir=$(dirname "$f")
  filename=$(basename "$f")
  extension=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
  filename="${filename%.*}"
  ln "$f" "${dir}/$filename Hardlink$extension"
done

EDIT: added what I think is better extension handling code - should be blank if no extension is present. Sorry, I am not good at coding scripts so please share any fixes you think this needs.

1
  • Pretty good. Just a caveat: If you have a file that doesn't have any extension, you will get some really surprising results! (And if one of the containing directories has a period in the name, the surprise will manifest in a different form.)
    – Wildcard
    Oct 3, 2016 at 16:15
1

Yes, indirectly.

Automator let's you make services that run any shell script and also can ask you for a path to the destination for the hard link (since it won't be in the same location as the original presumably). I suppose you could default the link location somewhere like a desktop too.

Just add a step to pop up a dialog to enter or otherwise choose the destination and pass that to your ln command.

1

Nice solution provided by Daniel, but I suggest replacing his script code by the one below which preserves the file type.

for f in "$@"
do  
  ln "$f" "Plink-$(basename "$f")"
done
2
  • What does "not destructive for file type" mean?
    – mmmmmm
    Oct 11, 2014 at 14:15
  • I followed the patern that the Finder's "make alias" command uses. You can, of course, change the naming convention however you see fit.
    – Daniel
    Oct 11, 2014 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .