Linux Link File Operations __linux

Source: Internet
Author: User

1. Classification of Links:

Links are divided into soft links and hard links:

1> Soft Link:

Soft links are also called Symbolic links, which contain a path to another file. You can connect to arbitrary files and directories, you can connect to different file system files, you can connect to files that don't exist, and you can loop through yourself.

2> a hard link is another name for an existing file. Attention:

1 only files in the same file system can create hard links;

2 does not allow the creation of a hard link to the directory.

2. Create a link in the shell interface using the LN command

When we need to use the same file in a different directory, we don't need to put a file that must be the same in every directory we need, we just put the file in a certain directory, and then use the LN command link in the other directory to make it available, without duplicating the disk space. For example:

Ln–s/bin/less/usr/local/bin/less, where-S is the meaning of the Code name (symbolic).

If you use LS to look at a directory, found that there is a file behind a @ symbol, that is a file in the ln command generated, with the ls–l command to view, you can see the path of link displayed.

Here are two points to note:

The 1> ln command keeps the synchronization of each linked file, which means that no matter which one you change, the other files will change the same.

2> Ln is linked to soft links and hard links in two ways:

1 Soft link creation instruction: ln–s filename link_filename, meaning to create a link file named "Link_filename" pointing to "filename", it will only generate a A mirror image of a file, similar to the Fast key method under Windows.

2 Hard link creation instruction: ln filename link_filename, without parameter-s, it will generate a file of the same size as the source file in your chosen location.

The files remain synchronized, whether they are soft links or hard links.

Instructions parameter details:

-F: First delete files with dist file name when linking

-D: Allows system managers to hard link their own directories

-I: Ask before deleting files with the same file name as Dist

-N: Dist is considered a general file when soft links are made

-S: Soft link (symbolic link)

-V: Displays the file name before the link

-B: Backup files that will be overwritten or deleted when the link is made

-S SUFFIX: Add the backup file to the end of the SUFFIX

-V Method: Specify how the backup should be

--HELP: Show Auxiliary Instructions

--version: Display version

3. C program using the link function to create

The link function declares the following:

#include <unistd.h>

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

Function Description: Link () Creates a new link (hard link) with the name specified by the parameter NewPath to the existing file specified by the parameter oldpath. If the parameter newpath the specified name as an existing file, the link is not established.

Return value: Success returns 0, failure returns-1, the reason for the error is in errno.

Error code:

1, exdev parameter OldPath and NewPath are not based on the same file system.

2, eperm parameter OldPath and NewPath refers to the file system does not support hard connection

3, Erofs file exists in the read-only file system

4, the Efault parameter oldpath or NewPath pointer exceeds the accessible memory space.

5, Enametollong parameter OldPath or NewPath too long

6, Enomem core memory is not enough

7, the Eexist parameter NewPath refers to the filename already exists.

8, the Emlink parameter OldPath refers to the file has reached the maximum number of connections.

9, eloop parameter pathname have too many symbolic connection problem

10, the ENOSPC file system's remaining space is insufficient.

11, Eio I/O access errors.

Example:

/* Establish/etc/passwd hard connection for pass * *

#include <unistd.h>

Main ()

{

Link ("/etc/passwd", "pass");

}

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.