4

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?

1
  • 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, 2017 at 14:52

2 Answers 2

8

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 have two options to create this feature:

  • by using Nautilus Actions or
  • by using Nautilus Scripts

Solution 1: by using 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 Legend 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/.

Solution 2: by using Nautilus Scripts

The same result can be achieved via a script, placed 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 the command:

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

The result will be:

enter image description here

References:

7
  • 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? May 7, 2017 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, 2017 at 21:50
  • @Programster, I've updated the answer with another approach.
    – pa4080
    May 8, 2017 at 9:47
  • Thanks - just a suggestion to move the script approach to the top - I almost missed it.
    – Mike McKay
    Sep 11, 2019 at 7:42
  • 1
    Thank you very much, I found the answer very useful. A slightly improved version of the script: -> [ ln "$1" "$(dirname $1)/Hard Link to $(basename $1)" ] - It works also when clicking within a file in a expanded tree.
    – CarlosRuiz
    Dec 20, 2021 at 10:50
3

Inspired by the answers here I wrote a Nautilus/Nemo script that makes hard links from multiple file selection.

#!/bin/bash
# make hard links for multiple file selection
# be careful of not moving the hard link to a different file system
HARDLINK_ICON=${HOME}/.local/share/icons/mine/folder-green-activities-icon.png
for arg 
do
    # make a hard link of the selected file
    if [[ -f "$arg" ]] 
    then
        HARDLINK_NAME="Hard Link to $arg"
        ln "$arg" "${HARDLINK_NAME}"
        gio set -t string "${HARDLINK_NAME}" metadata::custom-icon file:///${HARDLINK_ICON}
    fi
done
  1. Make sure of adding your icons to your local home folder. Something like ${HOME}/.local/share/icons/mine/folder-green-activities-icon.png
  2. Be mindful of not moving the hard link to a different file system; it will not work. If the icon doesn't show, it is most likely that cut-and-pasted the hard link to a different file system.
2
  • 1
    Thanks. This change makes the script work too with files in an expanded tree [ HARDLINK_NAME="$(dirname $arg)/Hard Link to $(basename $arg)" ]
    – CarlosRuiz
    Dec 20, 2021 at 11:14
  • This makes it work with expanded tree and file name with spaces in them: HARDLINK_NAME="$(dirname "$arg")/Hard Link to $(basename "$arg")"
    – Andrei B
    Mar 29, 2022 at 7:02

You must log in to answer this question.

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