0

I just started learning about the Linux command line with the book "The Linux Command Line". I was trying to create a hard link by following instructions from the book and typing this command: ln fun fun-hard. But I kept getting this result

ln: fun: hard link not allowed for directory

After doing some research, I found that hard links can't be created in directories. But if this is correct, why does the book include instructions for creating a hard link in a directory? Thanks for your help.

5
  • 3
    Check the ls output right after that. fun is clearly supposed to be a file. In fact it looks like fun was created a couple of sections previously by copying /etc/passwd, another file. How did you end up with fun as a directory following those instructions?
    – muru
    Aug 27, 2021 at 2:23
  • 2
    You misunderstood - "After doing some research, I found that hard links can't be created in directories" - i think you can't create a hardlink to directories.
    – Bonsi
    Aug 27, 2021 at 4:19
  • 2
    @ilkkachu you can find the book online (it's made available by the author under a CC license). It doesn't say you can make a hard link of a directory. In fact it quite specifically says "Hard links cannot reference directories, only files."
    – muru
    Aug 27, 2021 at 13:04
  • @muru, eh ok then. Apparently I made the mistake of trusting what the question said (and didn't read comments too closely, sorry)
    – ilkkachu
    Aug 27, 2021 at 13:23
  • 1

1 Answer 1

0

You tried to create a hardlink to a directory

root@rpiserver:~# mkdir fun
root@rpiserver:~# ln fun fun_hard
ln: fun: hard link not allowed for directory
root@rpiserver:~#

According to ln --help this is not possible

  -d, -F, --directory         allow the superuser to attempt to hard link
                            directories (note: will probably fail due to
                            system restrictions, even for the superuser)

Even as superuser with -d this failes here:

root@rpiserver:~# ln -d fun fun_hard
ln: failed to create hard link 'fun_hard' => 'fun': Operation not permitted

(All of this was tested here on a raspberry pi, using ext4 as file system)

1
  • This is the failed system call: linkat(AT_FDCWD, "fun", AT_FDCWD, "fun_hard", 0) = -1 EPERM (Operation not permitted)
    – Bonsi
    Aug 27, 2021 at 4:25

You must log in to answer this question.

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