Hard links and Symbolic links

Source: Internet
Author: User
Tags symlink touch command

Note: Only for learning Exchange, reprint please specify the source

Similar to Windows system shortcuts, Linux systems also provide a quick way to locate files in different directories. There are two kinds of links in Linux systems: one is hard link , and another is symbolic link .

Hard Links:

An inode node can have any number of hard links. When you delete a hard link, the data on the Inode node will not be affected. You can use the LN command to create a hard link on a Linux system. First, use the Touch command to create a file named Link.test. Then, enter the "ln link.test link" command, and then you create a hard link to link.test. The "ls–i" command allows you to see that the Inode node values for these two files are consistent.

Deleting the Link.test file still finds that the hard link file points to the Inode node. That is, the Inode node data has not been deleted, so to remove data from a hard linked file, you must delete all the hard links in addition to deleting the file itself.

Hard links are dependent on inode nodes, and there are several limitations to hard links in Linux systems:

(1): Only files can create hard links, directories cannot create hard links.

(2): Hard link cannot cross file system. You cannot create hard links to files that are located on different file systems (this is a file on a different partition).

 

 

Symbolic Link:

In Linux systems, the most used are symbolic links. Symbolic links are specialized file types that, unlike hard links, point to another file by name. As a result, there is no case where files and links in a hard link point to the same inode. Symbolic links also do not affect file deletion, and if the file disappears, the symbolic link becomes unavailable. Use "ln-s" to create symbolic links.

Symbolic links are much more flexible than hard links. In a Linux system, you can create symbolic links for any type of file system.

 

The following figure is a summary table of hard links and symbolic Links:

features

Hard Links

Symbolic Links

Name resolution SITUATION

Faster, because a hard link contains a direct reference to a linked object

Slowly, the symbolic link contains the path name of the linked object. Therefore, you must resolve the path name before you can find the linked object

Linked objects

Linked objects must exist before you can create hard links

Linked objects do not necessarily exist, when they do not exist you can create symbolic links

Whether a partition is required to exist in

Required in the same partition

Not required, symbolic links can span different file systems

Requirements for deleting linked objects

Deleting all the hard links will make it possible to delete related data

No request, delete the linked object, the symbolic link gives a pointing error

 

 

 

 

To Create or delete a link:

The link function is used to create a hard link. The specific information for this function is shown in the following table:

link Function

Header file

<unistd.h>

function form

int link (const char *oldpath, const char *newpath)

return value

Success

Failed

Whether to set errno

0

-1

Set up

Description: The link created here is a hard link, the calling parameter is mainly the original file name and the link name created. Similar to creating a hard link using ln, a link created cannot span the file system, and OldPath or NewPath cannot be a directory.

Error message:

Eacces: The process does not have permission to write to a file in the new directory, or the given file has no access to the path.

Eexist: The given NewPath already has a file.

Efault:oldpath or NewPath point to an illegal address space.

Eio: An I/O read/write error occurred.

Eloop: The number of symbolic links in the path to the given file is more than that.

Emlink: The link to execute OldPath has reached its maximum.

Enametoolong:oldpath or NewPath length is too long.

There is a problem with the directory in Enoent:oldpath or NewPath or an empty symbolic link.

ENOMEM: Insufficient kernel space.

ENOSPC: There is not enough disk space to create a new directory entry.

Enotdir: The given file contains a path that is not part of the directory.

Eperm:oldpath is the directory.

Eperm: File systems containing OldPath and NewPath do not support the creation of hard links.

Erofs: The file is on a read-only file system.

Exdev:oldpath and NewPath are not on the same mounted file system.

Instance:

#include <stdio.h>
#include <unistd.h>

int main (void)
{
        if (link ("./umask.c", "./1") = =- 1)
        {
                perror ("Cannot create the hard link");
                return (1);
        }

        return (0);
}


The Linux system also provides a unlink function to remove the hard links that are created. The specific information for the unlink function is shown in the following table:

unlink function

Header file

<unistd.h>

function form

int unlink (const char *pathname);

return value

Success

Failed

Whether to set errno

0

-1

Set up

Description: The unlink function deletes the link that pathname points to. If the link counter is 0 and no process has opened the file, unlink will free up the space occupied by the file.

Error message:

Eacces: The process does not have permission to write to a file in the new directory, or the given file has no access to the path.

Ebusy: The link was used by the system or another process and cannot be deleted.

Efault: The path points to an illegal address space.

Eio: An I/O read/write error occurred.

Eloop: The number of symbolic links in the path to the given file is more than that.

Enametoolong: path is too long.

Enoent: There is a problem with the directory in the path or an empty symbolic link.

ENOMEM: Insufficient kernel space.

ENOSPC: There is not enough disk space to create a new directory entry.

Enotdir: The given file contains a path that is not part of the directory.

Eperm: The system does not allow deletion of directories or permissions to delete directories.

Erofs: The file is on a read-only file system.

Eisdir: The path points to a directory.

Instance:

#include <stdio.h>
#include <unistd.h>

int main (void)
{
        if (unlink ("./1") = = 1)
        {
                perror ("Cannot create the hard link");
                return (1);
        }

        return (0);
}


To Create and delete symbolic Links:

By using the Symlink function, you can achieve the same result as the "ln-s" command, which is to create symbolic links. The specific definition of the Symlink function is the table:

symlink function

Header file

<unistd.h>

function form

int symlink (const char *oldpath, const char *newpath)

return value

Success

Failed

Whether to set errno

0

-1

Set up

Description: In the Symlink function, OldPath is the target to which the link is directed, and NewPath is the name of the link created.

Error message:

Eacces: The process does not have permission to write to a file in the new directory, or the given file has no access to the path.

Eexist: The given NewPath already has a file.

Efault:oldpath or NewPath point to an illegal address space.

Eio: An I/O read/write error occurred.

Eloop: The number of symbolic links in the path to the given file is more than that.

Enametoolong:oldpath or NewPath length is too long.

There is a problem with the directory in Enoent:newpath or an empty symbolic link.

ENOMEM: Insufficient kernel space.

ENOSPC: There is not enough disk space to create a new directory entry.

Enotdir: The given file contains a path that is not part of the directory.

Eperm: The file system containing NewPath does not support the creation of symbolic links.

Erofs: The file is on a read-only file system.

Instance:

#include <stdio.h>
#include <unistd.h>

int main (void)
{
        if (symlink ("./umask.c", "./1") = =-1)
        {
                perror ("Cannot create the hard link");
                return (1);
        }

        return (0);
}


the deletion of symbolic links is still implemented using the Unlink function described earlier.

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.