LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-11-2010, 09:20 PM   #1
jeffshen
LQ Newbie
 
Registered: Apr 2005
Posts: 7

Rep: Reputation: 0
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.

I am using centos 5.4.

Thank you very much in advance,

Jeff
 
Old 03-11-2010, 09:28 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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?



Cheers,
Tink
 
Old 03-11-2010, 09:30 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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()".

Make sense?

'Hope that helps .. PSM

Last edited by paulsm4; 03-11-2010 at 09:35 PM.
 
Old 03-11-2010, 10:22 PM   #4
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 297

Rep: Reputation: 62
Quote:
Originally Posted by jeffshen View Post
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.
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.
 
Old 03-11-2010, 10:50 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Code:
[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?"
 
Old 03-11-2010, 10:52 PM   #6
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 616

Rep: Reputation: 64
You can't create hard links if the two files exists in different file system.
 
Old 03-12-2010, 06:03 AM   #7
jeffshen
LQ Newbie
 
Registered: Apr 2005
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you for the very quick replies.

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.

Thank you very much in advance!
 
Old 03-12-2010, 06:05 AM   #8
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Rep: Reputation: 61
Hard links cannot cross filesystems - I'm assuming this is the problem. Do soft links work?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] rsync --link-dest not creating hard links on external usb drive quasi3 Linux - General 4 08-26-2009 11:11 AM
Copy File While File is Creating in Disk suhas7860 Linux - General 4 04-03-2007 09:38 AM
creating hard link problem cranium2004 Linux - Newbie 2 06-20-2005 06:44 AM
copy both soft link and its linked file okeyla Programming 3 02-14-2005 12:30 AM
Copy File as a Link Gerardoj Linux - General 3 12-02-2003 11:34 PM


All times are GMT -5. The time now is 08:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration