TL;DR: Making a hard link does not create a relationship between different files that causes new changes in one to be reflected in the other. Instead, it creates another name for the same file. Your link
command failed because you told it to create a link called file2.txt
, but file2.txt
already existed.
Like people and things in world, a file can have more than one name.
Often it is useful to have some way to refer to something. For some things, it is useful to have more than one way. This is not limited to computing.
For example, "acetaminophen" and "paracetamol" refer to the same medication. It is correct to say acetaminophen is paracetamol and equally correct to say paracetamol is acetaminophen. Neither name refers to the other -- instead, they refer to a particular chemical substance, which is not the same thing as any of its names.
Just as a word or phrase may identify a person or thing in natural language, a link -- also called a hard link -- identifies a file, and some files have multiple links. They aren't links to each other, but to the file. (To be precise, what they identify is the file's inode. See below.)
When there are multiple links to a file, it's not that the file has one true name and others are false or lesser names. Instead, all links provide names for the file, and none depends on the others. If someone asks you for the name of a file then, no matter which of its hard links you specify, you are equally correct.
When you ran link
, it did not succeed, and no link was created.
When link
told you link: file2.txt: File exists
, that was actually an error message. Because a file file2.txt
already existed, the link
command was unable to create a link by that name. Thus file1.txt
and file2.txt
are still separate files, and that is how they are able to have different contents.
The link
command does not create a relationship between separate files. It creates another name for the very same file. That name may be different, or reside in a different directory, or both, but it has the same relationship to the file as its original name. A common though somewhat imprecise shorthand you may have heard for this is that "a hard link is the file."
Links and Inodes
Although it's intuitive to many users that every file exists in a particular directory and nowhere else, and has just one name, those things are not true. Instead:
- The contents of a file may be physically spread out throughout the disk.
- Metadata about where to find the file's contents, as well as the owner, group owner, permissions, and any extended attributes of the file, are stored in a data structure called an inode. (An inode also contains some other information.)
- For each distinct file there is exactly one inode, and each inode is identified with a number. This inode number is the number shown in the left column when you run
ls -li
(and not at all when you merely run ls -l
). Two different files residing on the same device always have different inode numbers.
- It is useful to be able to think of a file as having a name, and as residing somewhere in a directory hierarchy. Therefore, a file may have one or more links (also called hard links, since there is something else called a "soft" or "symbolic" link). Each link has exactly one name and location. A file's links are links to the inode.
- A file's inode contains a count of how many links to it currently exist. When a new link is created, this count is incremented; when a link is removed, it is decremented. When it is decremented to zero -- that is, when the last link is removed -- then the file has been deleted.
- The
ln
(without -s
), cp -l
, and link
commands create an additional link to an existing file. For example, link file1.txt file2.txt
requires that a link file1.txt
already exist and that no link file2.txt
exist, and it creates a new link file2.txt
to the same file that file2.txt
links to. Most of the time it is sufficient to say that "file2.txt
is a link to file1.txt
." It is equally correct to say, "file1.txt
is a link to file2.txt
." Internally neither points to the other, but instead both point to the same inode.
The rm
and unlink
commands remove links. When we run rm foo
, we tend to think of this as deleting foo
, but no data in a file is lost and no disk space corresponding to the file is freed until all links to a file are removed. Often a file has just one link, but not always.
(Remember, however, that a symbolic link is not the same thing as a hard link. A symlink to a file does not prevent data loss when the file is deleted!)
Making Another Link to a File
To successfully experiment with hard links, you can ensure file1.txt
does exist and file2.txt
does not exist. Then when you run link file1.txt file2.txt
, you will have two links, file1.txt
and file2.txt
. When you run ls -li
you will see that they have the same inode number.
Now file1.txt
and file2.txt
are the same file.
Finally, you may want to use ln
instead of link
. The ln
command is more commonly used, safeguards against some errors, and, on some systems, has nicer error messages. (When you use the ln
command without the -s
flag, it creates hard links, as does the link
command. Passing the -s
flag causes to create a symbolic link instead.)
Further Reading
file1.txt
andfile2.txt
both exist when you ranlink file1.txt file2.txt
? Are they in the current directory? If they both existed,link
should have failed withlink: cannot create link 'file2.txt' to 'file1.txt': File exists
. Please [edit] with complete and exact output ofcat file1.txt
,cat file2.txt
, andls -li file1.txt file2.txt
. The-i
flag makesls
display a column for inode number, revealing whether or notfile2.txt
was really successfully created as a hard link tofile1.txt
. A hard link is just another name for the file and so always has the same contents. – Eliah Kagan Jan 18 '17 at 19:45link
file1.txt file2.txt
was successful then it is impossible forfile1.txt
andfile2.txt
to have different contents, because they are just two different names for the same file. – AlexP Jan 19 '17 at 04:17