LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 08: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, 08:28 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 08: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 08:35 PM.
 
Old 03-11-2010, 09:22 PM   #4
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 299

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, 09:50 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 09:52 PM   #6
Sayan Acharjee
Member
 
Registered: Feb 2010
Location: Chennai, India
Distribution: Manjaro
Posts: 624

Rep: Reputation: 64
You can't create hard links if the two files exists in different file system.
 
Old 03-12-2010, 05: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, 05:05 AM   #8
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

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


Reply



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 10:11 AM
Copy File While File is Creating in Disk suhas7860 Linux - General 4 04-03-2007 08:38 AM
creating hard link problem cranium2004 Linux - Newbie 2 06-20-2005 05:44 AM
copy both soft link and its linked file okeyla Programming 3 02-13-2005 11:30 PM
Copy File as a Link Gerardoj Linux - General 3 12-02-2003 10:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:59 PM.

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
Open Source Consulting | Domain Registration