Creating a hard link using ln creates a copy of file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Creating a hard link using ln creates a copy of file
Dear all,
I am trying to use ln to create a hard link to file a and whenever I do it, it creates a copy of the file instead. After having edited file a, when opening the link, it shows the old information and opening file a shows the new information.
The command I am using is
Code:
ln /home/user/file
within the new directory i am trying to link from.
Are those directories on the same mountpoint? Otherwise hard links can't
be done to begin with; even so, the behaviour you're describing doesn't
really gel ... what file-systems are you using, how are they mounted, can
you see anything odd if you try that ln under strace?
I'm not sure what you're trying to do, or what you're actually seeing, but:
Quote:
# Here's an example file:
$ cat a
AAAAAAAA
AAAAAAAA
AAAAAAAA
$ ls -l a
-rw-r--r-- 1 paulsm users 27 Mar 11 19:39 a
# Let's create a hard link:
$ ln a b
# Finally, let's check the results:
$ ls -l a b
-rw-r--r-- 2 paulsm users 27 Mar 11 19:39 a
-rw-r--r-- 2 paulsm users 27 Mar 11 19:39 b
$ cat b
AAAAAAAA
AAAAAAAA
AAAAAAAA
So what does this mean?
All you're doing when you create a file is:
a) assign some properties (like a meaningful name and a directory location) to a set of "inodes".
All you're doing when you create a hard link is:
b) assign a NEW "name" to the SAME set of "inodes"
Neither name is the "real" name. You can delete one link (for example, "a") or the other link ("b"); the file itself remains - untouched - until you remove the last link. All names are "equal" to the operating system.
Interestingly, the OS-level API for "delete a file" is "unlink()".
I am trying to use ln to create a hard link to file a and whenever I do it, it creates a copy of the file instead. After having edited file a, when opening the link, it shows the old information and opening file a shows the new information.
The command I am using is
Code:
ln /home/user/file
within the new directory i am trying to link from.
If the command that you have shown us is the actual command that you are using, then you are invoking ln incorrectly. ln requires two arguments - target file and linkname.
Do a full directory listing of the old file. If the link count is 1, then you have not successfully created the hard link.
[mherring@Ath stuff2]$ ls -l
total 4
-rw-r--r-- 1 mherring users 200 Mar 11 19:43 alph
[mherring@Ath stuff2]$ ln alph alphlink
[mherring@Ath stuff2]$ ls -l
total 8
-rw-r--r-- 2 mherring users 200 Mar 11 19:43 alph
-rw-r--r-- 2 mherring users 200 Mar 11 19:43 alphlink
[mherring@Ath stuff2]$ rm alph
[mherring@Ath stuff2]$ ls -l
total 4
-rw-r--r-- 1 mherring users 200 Mar 11 19:43 alphlink
[mherring@Ath stuff2]$ ^C
[mherring@Ath stuff2]$
Note the following:
1. The new hard link "alphlink" is indistinguishable from alph. This is because the hard link is simply another name for the same inode.
2. Deleting alph leaves alphlink intact. (same reason as above)
Excercise for the student: "When and how is a hard link different from a copy?"
I am not sure whether the directories are in the same hard drive as this is a hosting service provided by a company, but the file is within /home/usera/file and i am trying to access it from /home/userb/
I have tried using
Code:
ln /home/usera/file /home/userb/file
as well.
doing ls -li gives me 2 different inode numbers for the file in the different directories, but are usually within a few digits of each other, ie. 12156936 and 12156932.
The file i am trying to link has permissions of 777 so I cant think of why it cant link.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.