Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A hard link is defined as a pointer to an inode. A soft link, also known as a symbolic link, is defined as an independent file pointing another link without the restrictions of hard link.

My question is simply what is the difference of a file and a hard link ? Hard link is pointing to an inode, so what is a file ? Inode entry itself ? Or an Inode with a hard link ?

Let's say, I create a file with touch, then an Inode entry is created in the Inode Table. And I create a hard link, which has the same Inode number with the file. So did I create a new file ? Or the file is just defined as an Inode ?

share|improve this question
    
This is almost certainly a duplicate of unix.stackexchange.com/questions/9575/… – infixed 11 hours ago
1  
@infixed Exactly not, I am asking the difference of a file and a hard link. – Levent Divilioglu 11 hours ago
    
So I've undeleted my original answer that I believe was also covered in the answers to that linked question. So is it still 'exactly not'? – infixed 10 hours ago
    
The difference between a file and a hardlink is the same as the difference between you and the line with your name in the phonebook. – Jörg W Mittag 18 mins ago

A hard link is a directory entry. A file may have multiple directory entries, if it's present under different names or in different directories. A directory entry is called “hard link” when it's put in relation with other directory entries for the same file.

The inode contains the file's metadata other than its name and contents (location of the contents, permissions, timestamps, etc.). There's one inode per file. (Not all filesystems put the metadata in a clearly identifiable space on disk that you could call “inode”, but it's a common architecture.) A directory entry links a name to an inode. It's possible for more than one directory entry to link to the same inode, hence the term “link”. Such a link is called a “hard link” by opposition to “soft links” or “symbolic links” which don't say “for this name, use this inode” but “for this name, look up that other name”.

Think of files as rooms and directory entries as doors. “Open the file /foo/bar” means “go to corridor /foo and go to room bar”. “Go to room bar” really means “open the door marked bar and enter the room” but “go to room bar” is an unremarkable way to say the same thing in a shorter way. It's possible to have more than one door leading to the same room.

When you create a hard link to an existing file (ln existing new), you're creating a second link to the same file, i.e. you're creating a new directory entry that links to the already-existing file. After creation, the two directory entries have equal status: there isn't one that is “primary” and one that's “secondary”, they're just both links to the same file.

You can also remove all the links to a file without removing the file itself. This happens if you delete a file (i.e. you remove all its directory entries) while a program still has the file open. The file remains on the filesystem, it's only actually removed when the last process that had the file open closes it. In the room-and-doors metaphor, a room that has no doors still takes up space.

share|improve this answer
    
when were hard and soft links first introduced, respectively? – n611x007 7 hours ago

In the early days of Unix, the files internally were inodes on a particular disk drive. The file names were a more friendly way to access them.

A hard link was assigning more than one file name to an inode. You could create a file, hard link a second name to it and delete the first name and it was indistinguishable from simply having made the file with the second name in the first place.

Indeed, the system call that a program needs use to delete a file is 'unlink(2)`. The data doesn't go away until the last name is unlinked from the inode. (and the inode isn't open by a process somewhere)

This is what makes it easier for Linux to upgrade things while still running programs. If a process is running an executable, and an update happens then the program name gets reused, but the inode containing the old version still exists so it can continue to run. And when the last process running that old version stops, that old version storage is released.

Soft links came about because when you have a unitary file tree, with multiple mount points, you couldn't make a hard link from one hard drive to an inode on another another. So soft links were invented.

share|improve this answer
    
I guess this is a duplicate of unix.stackexchange.com/questions/9575/… – infixed 11 hours ago
1  
early days why is it any different now? your answer doesn't seem to reflect that view anyhow? – n611x007 7 hours ago

A file is the data written on the disk. This data is referenced by its inode, which contains metadata about the file telling the system what blocks on the disk are used by this file, among other things. A hard link points to the inode number of this file.

So technically, yes, you are creating a new file, but all this file contains is the inode number for the file it references and a it's name. It's better to think of it as creating a pointer to the inode, or a pointer to the file.

share|improve this answer

File is a widely used concept about entries in a filesystem.

Usually it includes Directory, Regular File (hard link), and Symbolic Link (soft link). And may even include device and socket.

My question is simply what is the difference of a file and a hard link ? Hard link is pointing to an inode, so what is a file ? Inode entry itself ? Or an Inode with a hard link ?

Let's say, I create a file with touch, then an Inode entry is created in the Inode Table. And I create a hard link, which has the same Inode number with the file. So did I create a new file ? Or the file is just defined as an Inode ?

Since even symbolic link is usually counted as file, a hard link itself can also be counted as a file. You can say it's a file regardless of whether it's a hard or soft link.

The concept is a bit ambiguous so it's also okay to say that an inode entry is a file, though you may actually want to refer to the data.

If you are a C++ or Java programmer you might want to read about std::filesystem::file_type, java.io.File, and java.nio.file.Files.

Details about differences between hard link and soft link can be found in the link in infixed's comment.

share|improve this answer

The very short answer is:

  • a file is an anonymous blob of data
  • a hardlink is a name for a file
  • a symbolic link is a special file whose content is a pathname

Unix files and directories work exactly like files and directories in the real world (and not like folders in the real world); Unix filesystems are (conceptually) structured like this:

  • a file is an anonymous blob of data; it doesn't have a name, only a number (inode)
  • a directory is a special kind of file which contains a mapping of names to files (more specifically inodes); since a directory is just a file, directories can have entries for directories, that's how recursion is implemented (note that when Unix filesystems were introduced, this was not at all obvious, a lot of operating systems didn't allow directories to contain directories back then)
  • these directory entries are called hardlinks
  • a symbolic link is another special kind of file, whose content is a pathname; this pathname is interpreted as the name of another file
  • other kinds of special files are: sockets, fifos, block devices, character devices

Keeping this metaphor in mind, and specifically keeping in mind that Unix directories work like real-world directories and not like real-world folders explains many of the "oddities" that newcomers often encounter, like: why can I delete a file I don't have write access to? Well, for one, you're not deleting the file, you are deleting one of many possible names for the file, and in order to do that, you only need write access to the directory, not the file. Just like in the real world.

Or, why can I have dangling symlinks? Well, the symlink simply contains a pathname. There is nothing that says that there actually has to be a file with that name.

My question is simply what is the difference of a file and a hard link ?

The difference between a file and a hard link is the same as the difference between you and the line with your name in the phone book.

Hard link is pointing to an inode, so what is a file ? Inode entry itself ? Or an Inode with a hard link ?

A file is an anonymous piece of data. That's it. A file is not an inode, a file has an inode, just like you are not a Social Security Number, you have a SSN.

A hard link is a name for a file. A file can have many names.

Let's say, I create a file with touch, then an Inode entry is created in the Inode Table.

Yes.

And I create a hard link, which has the same Inode number with the file.

No. A hard link doesn't have an inode number, since it's not a file. Only files have inode numbers.

The hardlink associates a name with an inode number.

So did I create a new file ?

Yes.

Or the file is just defined as an Inode ?

No. The file has an inode, it isn't an inode.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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