1

Say I create a hardlink with ln x y.

Is there a way to remove the link at y without removing the file at x, after I create the link? It looks like I did a rm -rf on y and it deleted x as well, would like to avoid that if possible lol.

7
$ echo 'hello' >name
$ ln name othername
$ ls -l
total 4
-rw-r--r--  2 kk  wheel  6 Jun 25 09:45 name
-rw-r--r--  2 kk  wheel  6 Jun 25 09:45 othername
$ cat othername
hello
$ rm name
$ ls -l
total 2
-rw-r--r--  1 kk  wheel  6 Jun 25 09:45 othername
$ cat othername
hello

You're doing something seriously wrong if, by removing one hard link, you also remove the other. Using rm or rm -rf in this case does not matter, nor does it matter which name is removed.

Removing a hard link just removes one of the names of the file. The only way I can see that rm -rf could remove both names is if you used it with a filename glob that matched both names, or if you deleted the directory that contained them both.


It appears, from comments, that you are actually using an application that creates a hard link for a directory. This is possible on macOS HFS+ filesystems under certain conditions. It is generally really fiddly to work with hard linked directories, and their use is largely limited to things like Apple's own TimeMachine software.

Related Stackoverflow question: What is the Unix command to create a hardlink to a directory in OS X?

It is pointed out in amongst the answers to that question, that a hard linked directory needs to be deleted using unlink rather than rm -r since rm -r would delete the directory's contents (under all its names).

See also: Forcibly create directory hard link(s)?

3
  • I am actually on MacOS using a custom utility (I didn't write it), but I am just making sure on linux this problem shouldn't happen. ln x y doesn't work on MacOS so there are some custom fill-ins for it - in my case I am using this - github.com/selkhateeb/hardlink Sep 10 '17 at 7:39
  • Maybe there is a bug in this utility that I am using - github.com/selkhateeb/hardlink Sep 10 '17 at 7:44
  • 4
    @AlexanderMills You can't create hard links to directories on Linux in general.
    – Kusalananda
    Sep 10 '17 at 7:46
1

Links are new file names pointing to the same file. Creating a hard link is creating a new directory entry (file name) pointing to the original file inode. That is the reason why you cannot hard link across filesystems and why metadata (permissions, xattrs, etc) are same for both links. On Linux creating hard links is only possible files, not directories.

The number of directory entries is tracked by link count in inode. When a hard link is created the link count is incremented. When deleting a file the directory entry is removed and link count decremented. You can check the link count in ls -l output 2nd column. Deletion of a single directory entry (link) doesn't remove the others.

If you created a new hard link in the same directory which contains the target file, something such as rm -rf * would obviously delete both links (and the space used by the file would be reclaimed by the filesystem if the link count goes 0).

1
  • by same directory, you mean same parent directory? Obviously any two files do have the same grandparent directory which is /. In my case, they did not share the same parent directory. Sep 10 '17 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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