How to undo hardlinks?

The best solution for finding and removing duplicate files.
Post Reply
rogeriodec
Posts: 4
Joined: Tue Apr 28, 2020 9:48 pm

How to undo hardlinks?

Post by rogeriodec »

I created some hard links from some files.
How to undo the hard link operation? :?:
User avatar
therube
Posts: 615
Joined: Tue Jun 28, 2011 4:38 pm

Re: How to undo hardlinks?

Post by therube »

I suppose you would have to first copy the hard linked file, then delete the file that was the hard link.

So, if you had:

file1.txt

& you created a link to that

file2.txt

file1 & file2 are now one in the same.

Make a copy of file2, 'file2 - Copy.txt'.

You now have:

file1.txt
file2.txt
file2 - Copy.txt

At that point, file1.txt & file2.txt are one in the same (still hard linked), & 'file2 - Copy.txt' is it's own file (though it happens to be a copy of them).

Now that you have that individual file, 'file2 - Copy.txt', delete file2.txt.

With that, you'll then have:

file1.txt
file2 - Copy.txt

They are both individual files, not linked in any manner.
(Their contents happen to be the same, but they are not the same file, are not hard linked.)

---

Experiment.
Make some hard linked files to test with, & understand what that entails.
Maybe go back to file1 & file2...

so file1 (hard linked to file2) contains the word, "hi".
since it is a hard link to file2, file2 also says "hi". (they are one in the same)
create a copy of file2, 'file2 - Copy'.
at that point, file2 - Copy' also contains the word, "hi".

all 3 files contain identical contents, yet file1 & file2 are one in the same, 'file2 - Copy' is its own individual file - not linked to anything else.

now edit file1, changing the word "hi" to "bye".

with that...

file1 says, "bye".
file2, a hardlink of file1 says, "bye"
file2 - Copy, its' own individual file, says "hi"

at that point, delete file2

file1 & file2, previously linked, the file2 reference now "unlinked"

with that, you now have...

file1, that says "bye"
& 'file2 - Copy', that says "hi"

each are their own individual files.
Post Reply