Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
ln copies a file instead of setting a hard link [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
cyberpatrol
Apprentice
Apprentice


Joined: 18 Sep 2003
Posts: 161
Location: Germany

PostPosted: Tue Nov 01, 2005 12:17 am    Post subject: ln copies a file instead of setting a hard link [SOLVED] Reply with quote

I have a problem with ln.

I tried to set a hard link to a file by giving this command:
ln /usr/dir1/file /usr/dir2/file
or
cd /usr/dir2 && ln /usr/dir1/file

Both /usr/dir1 and /usr/dir2 are on the same partition and therefore on the same filesystem.
The filesystem is reiserfs.

Instead of setting a hard link in /usr/dir2 to /usr/dir1/file it copies file from /usr/dir1 to /usr/dir2.

Why does this happen and how I can set a hard link?


Last edited by cyberpatrol on Tue Nov 01, 2005 5:09 am; edited 1 time in total
Back to top
View user's profile Send private message
paul555
l33t
l33t


Joined: 22 Nov 2004
Posts: 612
Location: Greece

PostPosted: Tue Nov 01, 2005 12:27 am    Post subject: Reply with quote

Code:
man ln
is your friend.To set a hard link to a file you just do
Code:
ln -s

_________________
"LINUX, MS-DOS, Windows : known as the Good, the Bad and the Ugly." :)
http://www.gnome.gr
Back to top
View user's profile Send private message
Jake
Veteran
Veteran


Joined: 31 Jul 2003
Posts: 1132

PostPosted: Tue Nov 01, 2005 12:29 am    Post subject: Reply with quote

Are you sure you understand how a hard link is supposed to behave? It will look like the original in every way, except it won't store a copy of the data. If you delete the original, a hard link will still point to the data. For example:
Code:
$ mkdir test
$ cd test
$ dd if=/dev/zero of=orig bs=8k count=1
$ du -sh .
8.5K    .
$ ln orig link
$ du -sh .
8.5K    .
$ ls -l
total 16
-rw-r--r--  2 jake users 8192 Oct 31 18:23 link
-rw-r--r--  2 jake users 8192 Oct 31 18:23 orig
$ rm orig
$ du -sh .
8.5K    .
$ ls -l
total 8
-rw-r--r--  1 jake users 8192 Oct 31 18:23 link
$

In most cases you'll want a soft link.
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Tue Nov 01, 2005 12:44 am    Post subject: Reply with quote

Jake said it fine For all purposes, the file and the hardlink are the same, since both point to the data on the hd, instead of the link pointing to the file. So, if you remove one the other will stay. They are both valid names for the same data on the disk. The symbolic or soft links are different. In a symlink the link points to the original file (the name, to explain it on an easy way) but not to the data itself.

@Paul, -s is for a soft (or symbolic) link, hence the '-s', also called 'symlink'.
Back to top
View user's profile Send private message
cyberpatrol
Apprentice
Apprentice


Joined: 18 Sep 2003
Posts: 161
Location: Germany

PostPosted: Tue Nov 01, 2005 12:48 am    Post subject: Reply with quote

In this case I indeed need a hard link and not a soft link.
And I think I understand how a hard link is supposed to behave.

The problem is if I edit the file to which I set a hard link the hard link should contain the same changes and vice versa.
E.g. if I set a hard link with `ln /usr/dir1/file /usr/dir2/file` and I edit /usr/dir2/file (the link) then `cat /usr/dir1/file` and `cat /usr/dir2/file` should show the same content.
Or am I wrong in this point and this is only the behaviour of symlinks?

I have to admit that I'm not sure right now?

Or let me reword it. How can I find out if a "file" is a copy or a hard link?
Back to top
View user's profile Send private message
Jake
Veteran
Veteran


Joined: 31 Jul 2003
Posts: 1132

PostPosted: Tue Nov 01, 2005 1:28 am    Post subject: Reply with quote

So you mean if you did
Code:
$ mkdir dir1
$ touch dir1/file
$ mkdir dir2
$ ln dir1/file dir2/file
$ echo blah > dir2/file
$ cat dir1/file
blah
$

you wouldn't see "blah" at the end?

EDIT: I'm using reiser4


Last edited by Jake on Tue Nov 01, 2005 1:40 am; edited 1 time in total
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Tue Nov 01, 2005 1:36 am    Post subject: Reply with quote

cyberpatrol wrote:
In this case I indeed need a hard link and not a soft link.
And I think I understand how a hard link is supposed to behave.

The problem is if I edit the file to which I set a hard link the hard link should contain the same changes and vice versa.
E.g. if I set a hard link with `ln /usr/dir1/file /usr/dir2/file` and I edit /usr/dir2/file (the link) then `cat /usr/dir1/file` and `cat /usr/dir2/file` should show the same content.
Or am I wrong in this point and this is only the behaviour of symlinks?

I have to admit that I'm not sure right now?

Or let me reword it. How can I find out if a "file" is a copy or a hard link?


You are not wrong, if you did a 'ln file1 file2' then file2 should contain exactly the same that file1, and vice-versa, does not matter wich one of the two do you edit, since both point to the same location in the block device. Even most, I don't know if there is any easy way to find if a file is or not a hardlink, since at all effects, hardlinks are just another name for the same file, and not just a link. Even if you make a 'ls -l' after updating the contents of a hard linked file, then you will notice that the modification/creation date of the original file and the hardlink are the same, both are modified to match the last edition date.

I can't test in a reiser filesystem, because I don't use reiser at all. I would not be surprised if your problem it was reiserfs related, since there are some other reports of similar things not working as they should. In any case, don't you have another fs to try? If you dont have one you could make a loopback device or format a floppy with ext2 just to check if it makes any difference...
Back to top
View user's profile Send private message
cyberpatrol
Apprentice
Apprentice


Joined: 18 Sep 2003
Posts: 161
Location: Germany

PostPosted: Tue Nov 01, 2005 1:46 am    Post subject: Reply with quote

This is an idea. I still have an empty partition which I could format with ext2 and test it.
I could have thought of by myself.

Btw, after setting the hard link I edited the "link" but the "link" was changed and the "original" file was not. So it must be copy instead of a link.
I will test it on ext2 and maybe ext3.
Back to top
View user's profile Send private message
Jake
Veteran
Veteran


Joined: 31 Jul 2003
Posts: 1132

PostPosted: Tue Nov 01, 2005 2:16 am    Post subject: Reply with quote

The second column in the "ls -l" output is the number of hard links to a file. If two of your files show link counts of "2", you won't know which is the original, but you'll know that each has another link.

Just an idea, but have you tried running "sync" after editing the link?

And if possible, please post a complete comand sequence producing this behavior.
Back to top
View user's profile Send private message
cyberpatrol
Apprentice
Apprentice


Joined: 18 Sep 2003
Posts: 161
Location: Germany

PostPosted: Tue Nov 01, 2005 4:17 am    Post subject: Reply with quote

I've found the error. It was my fault.

ln hasn't copied the file. In fact it has set a hard link.

But because I forgot the thing with the link count of 2 in the ls -l output I tried to edit the file(s) and looked if both files have been changed.
The mistake was that I used emacs to edit the files and that emacs automatically saves a backup. It renames the original file to <filename>~ - which I often delete at once - and saves the edited file as a copy and names it <filename>.

So after editing the hard link points to <filename>~ and not to <filename> anymore. This is why the files were different.

But thank you all for explaining the hard link stuff.
I new it once but have forgotten it again because I usually don't use hard links.
Now it's clear again and I hope I won't forget it again. :-)

Thanks again!
Back to top
View user's profile Send private message
Jake
Veteran
Veteran


Joined: 31 Jul 2003
Posts: 1132

PostPosted: Tue Nov 01, 2005 4:58 am    Post subject: Reply with quote

I would never have thought emacs would screw up hard links. *cough* vi *cough*

Remember to append [SOLVED] to the subject of your original post.
Back to top
View user's profile Send private message
cyberpatrol
Apprentice
Apprentice


Joined: 18 Sep 2003
Posts: 161
Location: Germany

PostPosted: Tue Nov 01, 2005 5:19 am    Post subject: Reply with quote

Done.

Maybe I should send a bug report to the emacs developers. But a bit later.
If emacs would first copy the original file <filename> to <filename>~ and then save the edited file to <filename> then it shouldn't screw up hard links again.

But I'd never thought that, too.

Btw, nano is a small but also a quite nice and easy editor which can also handle hard links.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum