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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
$ 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 -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)?
I know this is an old post but hard links only work with file but you can use copy with a hard link option to make a recursive copy and it does work in Mac OS as well, though it gives warning messages on the Mac that can be ignored.
cp -Ral x y
-R is for recursive, the
-a is for attributes, the
-l is for link.
After that you could
rm -rf x or
rm -rf y and the other would remain.
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).