ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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");"?
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*
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.