LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-06-2004, 12:14 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45
differences betwn link, unlink, mkdir, rm


is the function "link" the same as doing a mkdir?
is "unlink" the same as "rm -rf dirname"?
thanks.
 
Old 01-06-2004, 12:23 PM   #2
stv_t
Member
 
Registered: Oct 2003
Location: Gatwick, UK
Distribution: Mandrake 9.1, Mandrake 9.2, Redhat 9.0
Posts: 47

Rep: Reputation: 15
No -
link creates a hard or soft link to the file or directory specified as an argument
unlink unlinks a file from a directory structure by calling the unlink system function- it should NOT be used in place of 'rm' except when 'rm' cannot remove a file, sometimes happens but not often.

The man and info pages should explain all this try
man link
info unlink

stv t
 
Old 01-06-2004, 12:29 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 45
thanks stv.
im on a solaris, and the only manpages available for link/unlink are from the fortran lib. and that doesnt really say anymore than you did.
isnt creating a hard link the same as creating the file? i understand that the soft link is a symlink and is pretty much the same as a pointer to the file, but not sure about the hard link.
and doesnt rm remove the file from the dir structure?

so you're saying that they arent equivalent to "system("rm -rf dir");" and "system("mkdir dir");"?
 
Old 01-06-2004, 01:29 PM   #4
stv_t
Member
 
Registered: Oct 2003
Location: Gatwick, UK
Distribution: Mandrake 9.1, Mandrake 9.2, Redhat 9.0
Posts: 47

Rep: Reputation: 15
Hi,

Ok - Here goes:

A hard link as created by ln file_1 file_2 creates a directory entry which points to the original file. Any number of these links can be created with each one pointing to the same physical file on the disk.
A hard link can only be created if the file it is linked to already phsically exists on the disk.
A hard link cannot be created across different partitions.
A hard link cannot be differentiated from an ordinary file.

When running an ls -l on the original file and the link to the file the output will be exactly the same including the size and the creation date of the file and the link ie.
prompt> ln rs rs0
-rw-rw-r-- 2 stv stv 1373 Jan 6 16:58 rs
-rw-rw-r-- 2 stv stv 1373 Jan 6 16:58 rs0


When deleting a hard link the original file is treated the same as a linked file:
The original file can be deleted but the linked files will still be able to access the contents of the file.
The contents of the hard linked file are only physically removed from the disk once the last reference to them is removed.


A soft link as created by ln -s file_1 file_2 creates a directory entry which contains a reference to the original file name. Any number of soft links can be created to a physical file, however they do not all have to reference the physical file in the same way ie if the original file is rs the following link commands
ln -s rs rs1
ln -s /home/stv/rs rs2
ln -s ./rs rs3

give the following results for ls -l rs*

-rw-rw-r-- 1 stv stv 1373 Jan 6 16:58 rs
lrwxrwxrwx 1 stv stv 2 Jan 6 18:58 rs1 -> rs
lrwxrwxrwx 1 stv stv 12 Jan 6 18:58 rs2 -> /home/stv/rs
lrwxrwxrwx 1 stv stv 4 Jan 6 18:59 rs3 -> ./rs


Notice that the size of the file is the same as the number of characters in filename pointed to. Therefore although they all point to the same file they have a different size. Also the first character of the permissions contains an l to indicate that the file is a soft link and also show the physical filename that the link refers to.
A soft link can cross partitions.
A soft link can be created to point to a file that does not yet exist. ie
ln -s no_file rs4 followed by
ls -l no_file rs4 results in
ls: no_file: No such file or directory
lrwxrwxrwx 1 stv stv 7 Jan 6 19:15 rs4 -> no_file


Trying to read the contents of this link will result in an error message saying that the file does not exist.
Writing to a soft link to a non-existant file will succeed and create the physical file ie cat rs > rs4 followed by
ls -l no_file rs4 results in
-rw-rw-r-- 1 stv stv 1373 Jan 6 19:17 no_file
lrwxrwxrwx 1 stv stv 7 Jan 6 19:15 rs4 -> no_file

If the physical file to which a soft link refers is removed then all soft links to that file will result in a No such file error.

rm is a system command which allows a number of options and safeguards
whereas unlink is simply a wrapper round the unlink system call which does not have any of the safeguards or options of rm.


Stv T
 
Old 01-06-2004, 01:38 PM   #5
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 45
thanks a lot for your time n patience - i got it now - nice!
 
Old 01-06-2004, 01:43 PM   #6
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 45
ohh - one last question. is there some particular reason as to why the concept of hard links was thought of (apart from a copy of the file?). im equating the symlinks as shortcuts to the files, and i can see many uses of that. but dont see why the concept of hard links exists? if it plays a large part, does it mean that a lot of functions dealing with files would make a call to link/unlink sooner/later? thanks.
 
Old 01-06-2004, 05:25 PM   #7
stv_t
Member
 
Registered: Oct 2003
Location: Gatwick, UK
Distribution: Mandrake 9.1, Mandrake 9.2, Redhat 9.0
Posts: 47

Rep: Reputation: 15
Hi

Hard links were developed before soft links, both are actually short cuts to the physical file. Soft links were developed to to enable files to be linked across file systems and hard disks, something which cannot be done with hard links.
Hard links do not depend on the initial filename being present whereas soft links refer to nothing if the initial filename is removed. Hard links are NOT a copy of the original file merely another name for the original.

stv t
 
Old 01-06-2004, 05:47 PM   #8
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 45
hey, thanks again.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RM: Cannot Unlink margolik Linux - Software 0 04-17-2004 11:03 AM
cannot unlink cannot remove Rob Ellsworth Linux - General 16 10-01-2002 04:15 PM
cannot unlink cannot remove Rob Ellsworth Linux - General 1 10-01-2002 11:27 AM
cannot unlink simon Linux - General 1 07-31-2001 07:25 PM
How to unlink a directory? _TK_ Linux - General 4 04-25-2001 08:32 PM


All times are GMT -5. The time now is 01:19 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration