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.

chaos
  • 47,463
  • 11
  • 118
  • 144
  • 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 '21 at 02: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 '21 at 04:19
  • 2
    @ilkkachu you can find [the book online](https://linuxcommand.org/tlcl.php) (it's made available [by the author under a CC license](https://linuxcommand.org/tlcl.php)). 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 '21 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 '21 at 13:23
  • 1
    [Why are hard links to directories not allowed in UNIX/Linux?](https://unix.stackexchange.com/q/22394/170373) – ilkkachu Aug 27 '21 at 13:24

1 Answers1

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)

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