The LN command is provided under the Linux system for file linking. File links are mainly divided into hard links and soft links.
Hard Link: Because the files under Linux are through index Node (Inode) to identify a file, a hard link can be thought of as a pointer to a pointer to a file index node, and the system does not redistribute the Inode for it. Each add a hard link, the number of links to the file is added 1.
You can use the: ln command to create a hard link. Grammar:
- ln [options] Existingfile NewFile
- Ln[options] Existingfile-list Directory
Soft links (Symbolic Links):
Soft links overcome the lack of hard links, without any file system restrictions, any user can create a symbolic link to the directory. It is now more widely used, it has greater flexibility, and can even link files across different machines and networks.
- $LN –s file1 File1soft
Program execution:
Link () function and symlink ()
Unlink ("/OPT/IPNC");
sprintf (CSRC, "/opt/ipnc%d", idirnum);
sprintf (CDST, "/OPT/IPNC");
Symlink (CSRC, CDST);
prototypes :
#include <unistd.h>
int symlink( Const Char * OldPath , Const Char * NewPath);
ssize_t Readlink( Const Char * Path , Char * buf , size_t Bufsiz);
Description :
The Symlink () function creates a symbolic link called NewPath that points to a file called OldPath. Symbolic links can span the file system and do not perform any checks on the presence of OldPath. Once you create a symbolic link, you can use the Readlink () function to read its contents to see where it points.
The Readlink () function allows us to determine what the symbolic link to path is pointing to. The first bufsiz byte of a pathname is copied to the user-supplied buffer buf. When the pathname is copied to buf, it does not terminate as NULL as we expected, so you must add your own termination token.
Sample Code :
#include <unistd.h>
#include <stdio.h>
int Main()
{
Char buf [ - ];
if( symlink( "/HOME/PROGRAMMING/SYSTEM/SYMLINK.C" , "/home/groad/symlink_test.c") == - 1)
perror( "Symlink");
Readlink( "/home/groad/symlink_test.c" , buf , 127);
buf [ - ] = ' + ';
printf( "%s \ n " , buf);
return( 0);
}
Run and output :
./symlink
/HOME/PROGRAMMING/SYSTEM/SYMLINK.C
The difference between:
A hard link is a backup, a soft connection is a shortcut
http://blog.csdn.net/hairetz/article/details/4168296
http://blog.csdn.net/stilling2006/article/category/746887