2

I made a hard linked file in terminal by command: ln somefile somefile_h and used it for fun. Now I faced a question that how can I delete the link between these two files and use it separately? Anybody know?

Aprime
  • 141

2 Answers2

4

To create independent files, do the following (assuming somefile is in your current directory):

cp somefile temp_somefile
rm somefile
mv temp_somefile somefile

This will create an identical file somefile, that is not anymore linked to somefile_h. No knowledge of where the linked file exists on your file system is required with this approach.

If you know where the linked file is, then it is shorter to delete that linked file and next make a regular copy of the (previously linked) file under the same file name:

rm <path>/somefile_h
cp somefile <path>/somefile_h

A way to achieve this with a cp single command was provided by user bac0n:

cp --remove-destination somefile <path>/somefile_h

Here, the destination (linked) file is deleted before making a copy. Thus, the copy becomes a "new", regular file that is not anymore linked to the source file.

vanadium
  • 88,010
1

Hard linked file is like name pointing to same inode.

To remove your hardlink, just

rm linked_name

That doesn't affect your original filename and data in disk, there is a inode reference counter that increments on linking and decrements when you unlink instance. When counter reaches zero, inode is removed and space is marked free.

To make copy, use cp and for link, use ln

Pasi Suominen
  • 1,004
  • 8
  • 11