1

I finally got round to studying and understanding hard links in my quest to check I have understood the basics, of which I have managed to skip a lot of. After realizing how brilliant they are, I wonder why there isn't better support for them in Ubuntu's default file manager, nautilus.

At the moment, there is only one option for creating a link to a file, and it will create a "soft" link.

enter image description here

The trouble with this is that they break as soon as you move or rename the original file.

Question

Is there a way to get nautilus to create a hard link, or some sort of addon/script/configuration/tweak I can do to add the option in the right-click menu, or a keyboard shortcut, to create a hard link for a file?

  • Hard links can be make using the ln command in terminal. But the question might be asked... why do you want/need hard links vs soft links? Do you understand the disk space ramifications of each? – heynnema May 7 '17 at 14:52
1

Apparently this option is intentionally cut years ago. From the topic Creating hard links from bugzilla.gnome.org:

Almost none of our users knows the hardlink concept, so why should we confuse him and even risk data loss? You can use nautilus-open-terminal and create hardlinks manually.


The good news is you can create this option with the help of the package Nautilus Actions.

1. First install the package nautilus-actions:

sudo apt update
sudo apt install nautilus-actions

Or use Ubuntu Software:

install nautilus-actions

2. Run the program:

run nautilus-actions

3. Go to 'Preferences' and uncheck 'Create root Nautilus Action menu':

nautilus-actions Preferences

When this option is ticked, in the context menu will have sub menu, etc.

4. Click on Define new action button and fill the data in the Action tab. As Context Label you can use Make Hard Link, also tick only the option Display item in selection context menu - in other words display this item when a file is selected.

Define new action - Action

5. Go to Command tab and fill the parameters:

  • Profile:

    • Label: Default profile - if there is no any custom profile.
  • Command:

    • Path: /bin/ln - this is path to the executable file (or command).
    • Parameters: %f 'Hard Link to %b' - click on Label button for more info.
    • Working directory: %d - means current directory.

Define new action - Command

6. Because Directory hardlinks break the filesystem in multiple ways we must disable this possibility. Go to Mimetypes tab and add a new rule:

  • Mimetype filter: inode/directory,
  • with Must not match any of option selected.

Define new action - Mimetypes

7. Record the action Make Hard Link:

Define new action - Record

8. Run Nautilus and use Make Hard Link action from the context menu:

enter image description here


Additionally, if you want to backup your settings:

  • the file $HOME/.config/nautilus-actions/nautilus-actions.conf contains Nautilus Actions preferences.
  • and the new actions are places within the folder: $HOME/.local/share/file-manager/actions/.

EDIT: Solution using script

Same result can be achieved via script, places into $HOME/.local/share/nautilus/scripts/.

This script could looks like:

~$ cat "$HOME/.local/share/nautilus/scripts/Make Hard Link"

#!/bin/bash

if [ -f "${1}" ]
then
        ln "$1" "Hard Link to $1"
fi

We can create this script and give to it executable permissions via single command:

SNAME="$HOME/.local/share/nautilus/scripts/Make Hard Link"; \
printf '#!/bin/bash\n\nif [ -f \"${1}\" ]\nthen\n\tln \"$1\" \"Hard Link to $1\"\nfi\n' |\
tee "$SNAME"; \
chmod +x "$SNAME"

The result will be:

enter image description here

References:

  • Hey, it works. It feels weird to have to perform a bunch of GUI based steps. Now that I have that set up, can I just copy/overwrite some config file in future? – Programster May 7 '17 at 20:16
  • Hay @Programster, we can use find command in this way: find $HOME -mmin -360 | grep action - to find all files, within the user's home directory, that are changed last 6 hours and whose names contains the string 'action'. I will update the answer tomorrow, I think we do not need 'Nautilus Actions' at all. Maybe just a .desktop file, placed into $HOME/.local/share/file-manager/actions/ will be enough. – pa4080 May 7 '17 at 21:50
  • @Programster, I've updated the answer with another approach. – pa4080 May 8 '17 at 9:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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