0

While reading about linux,I got the following:

Suppose that file1 already exists. A hard link, called file2, is created with the command:

$ ln file1 file2

Note that two files now appear to exist. However, a closer inspection of the file listing shows that this is not quite true.

$ ls -li file1 file2

The -i option to ls prints out in the first column the inode number, which is a unique quantity for each file object. This field is the same for both of these files; what is really going on here is that it is only one file but it has more than one name associated with it, as is indicated by the 3 that appears in the ls output. Thus, there already was another object linked to file1 before the command was executed.

Hard links are very useful and they save space, but you have to be careful with their use, sometimes in subtle ways. For one thing if you remove either file1 or file2 in the example on the previous screen, the inode object (and the remaining file name) will remain, which might be undesirable as it may lead to subtle errors later if you recreate a file of that name.

I am not getting the notion of file object as stated above as UNIX has been made purely in C(correct me If I am wrong). Is it somewhat similar to mutable and immutable classes in Java?

  • 1
    That something is coded in C doesn't mean objects can't exist. What are structs for? Further languages like Vala compile down to C, and have a high level of object-orientedness. – muru Sep 2 '14 at 12:38
  • Yeah Thanx,,Can we say C as a partial object oriented? – lazarus Sep 2 '14 at 15:56
1

No, but hard links works exactly as object references in Java. You can copy a reference to any number of variables, it will not duplicate the object. You can clear these variables or delete them, but the object will be cleared only when the last reference will be cleared (not the last created).

Hard links are the same: they're reference to the real file object, or in other words directory entries. Creating a hard link is creating another reference to the file object in another directory. When you rm a file somewhere, it doesn't destroy the file object if another reference (inode) to it exists somewhere else.

They're different from soft links (shortcuts) because deleting a shortcut never erase the file.

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.