RE: Example of using 'ln'
Posted: 6/18/2003 3:14:17 PM
By: Comfortably Anonymous
Times Read: 2,628
0 Dislikes: 0
Topic: Linux
Parent Message
Hmmm... OK, I should have stuck with 'ln -s', I never use ln without the -s.

I did a test and got curious results I'm not sure I understand:

I first created a file called test.txt containing 'blah blah blah'.
I then created a hard link to it with 'ln -s test.txt test2.txt'
I then deleted test2.txt with 'rm test2.txt', and saw that test.txt still existed. (I had expected test.txt to be gone along with test2.txt)
I then created a new hard link with 'ln -s test.txt test3.txt'
I then deleted the original file with 'rm test.txt'
I saw that test.txt was gone, but test3.txt was still there, containing the original 'blah blah blah' content of the previously-deleted test.txt file!

I'm not sure what that's all about. If someone else would like to explain here, it would be most appreciated! :)  I never use hard links, so don't know much about them
Rating: (You must be logged in to vote)
Discussion View:
Replies:

RE: Example of using 'ln'
Posted: 6/18/2003 3:14:17 PM
By: Comfortably Anonymous
Times Read: 2,628
0 Dislikes: 0
Topic: Linux
Although I am no expert in the subject, here is how I think of it (someone correct me if I am wrong): All files are essentially hard links, i.e. all files contain a link to an inode number that contains the data on the actual drive.  Use  ls -li  to view inode numbers... when you copy a file you create a brand new inode number and rewrite all of the data from the original filespace to the new one.  However, when you create a hard link you are simply duplicating the reference to the file's internal data.  So, when you deleted the original "test" file, you basically deleted one link to the inode.  However, the data still existed in the inode which is why the other hard links still funcitoned as they did prior.  I guess, then, that symbollic links function as links to links to data.  This way you can access the data pointed to by a symbolic link as you normally would with a file, but when you delete the original, you break the chain and the link ceases to function.  OK, now for some more fun  :)  Try creating a file, then creating a symbollic link to that file, and then creating a hard link to that symbollic link.  If you delete the symbollic link, the hard link continues to function, as you would expect.  However, if you delete the original file then the hard link becomes broken.  Seems to me like it's not a good idea to mix hard links and symbollic links because a hard link to a symbollic link turns out to be a symbollic link (?).
Rating: (You must be logged in to vote)

RE: Example of using 'ln'
Posted: 6/18/2003 3:14:17 PM
By: Comfortably Anonymous
Times Read: 2,628
0 Dislikes: 0
Topic: Linux
ok, I could be wrong here, but i thought "ln -s /blah/blah.txt blah.txt" would create a softlink (symbolic link, symlink) to the original blah.txt (and the original is located in the directory /blah/)

but doing "ln /blah/blah.txt blah.txt" would create a hard link to the same file.

and, if you create a symbolic link, then delete the original blah.txt (i.e. /blah/blah.txt) then the link you created "dangles" i.e. isn't linked to anything anymore.  

but, if you created a hard link, and then delete the hard link, the original file goes away too.  

however, what i'm not clear on is if i create a hard link, then delete the original file, does the hard link become a softlink?  or does it disappear too?  guess it's time to go test it out . . .
Rating: (You must be logged in to vote)