Linux file directory connection

Source: Internet
Author: User
Tags symlink

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:

    1. ln [options] Existingfile NewFile
    2. 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.

    1. $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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.