Create and delete soft and hard links under Linux

Source: Internet
Author: User
Tags symlink

Reference: https://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/05/2199534.html

Create and delete soft and hard links under Linux

In a Linux system, the kernel allocates an Inode (index node) for each newly created file, each with a unique inode number. The file attributes are stored in the index node, and the index nodes are copied to the file when they are accessed, enabling quick access to the file.

A link is a way to establish a connection between a shared file and a number of directory entries for the user who accesses it. There are two types of links in Linux: Hard links and soft links (Soft link), and soft links called Symbolic links (symbolic link). Symbolic connections are equivalent to shortcuts under Windows.

One, hard links

Hard links plainly is a pointer to a file index node, and the system does not redistribute the inode for it. You can use the: ln command to create a hard link.
Grammar:
ln [options] existingfile newfile (sexistingfile file for link file, NewFile is the newly created link file)
ln [options] Existingfile-list Directory

-F is created, it is deleted with the file name.
-I ask before deleting.

LN-S ABC CDE establishes the soft connection of ABC
LN ABC CDE establishes the hard connection of ABC,

Usage:
The first: Create a hard link for "Existingfile" with the file name "NewFile".
The second type: in the "directory" directory, create a hard link with the same name for all files contained in "Existingfile-list".
Common optional [options]–f the link is created regardless of whether the newfile exists or not. -N If "NewFile" already exists, no link is created.

Here are some examples:

[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
[Email protected] test]# LN file2 file2hand
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
[Email protected] test]#

Note Before you create a link, the number of links that File1 displays is 1, and the number of links file1 and File1hard are changed to 2;file1 and File1hard are the same in the inode number, as are the file sizes shown in File1 and File1hard. The operation results of the LN command are visible: File1 and File1hard are two names of the same file, they have the same index node number and file attributes, and a hard link to the file file1 is to create a new pointer to the File1 file index node in the current directory. You can delete any one of them, such as RM file2, and delete only one pointer at a time, minus one at a time, and the kernel will delete the file contents from disk when the number of links is reduced to 0 o'clock.

You can also create hard links to files in different directories but on the same file system. Set File1, file2 in the directory/home/root/dir1, the following command, in/home/root to establish a hard link file2.

ln File2/home/root/file2hard
The following program is to set up a hard link in the directory Dir2 for all files in the Dir1 directory
#mkdir Dir2
#ln/home/root/dir1/*/home/root/dir2

If the ln–f existingfile newfile is used, if the newfile already exists, the newfile becomes a hard-link file for NewFile, regardless of the original Exisitngfile file, only with the current user having write access to it.

Although hard links save space and are the traditional way for Linux systems to integrate file systems, there are shortcomings:
(1) No link can be established between files in different file systems
(2) Only Superuser can create a hard link for a directory.

Second, soft link (symbolic link)

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.
To create a soft link, just add the option –s after LN, for example

[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand

[Email protected] test]# ln-s file1 file1soft
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491140 lrwxrwxrwx 1 root root 5 07-14 14:24 File1soft-file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand

From the results of the link above can be seen in soft links and hard links, the difference is not only conceptually, in the implementation is also different. The difference: the hard link original file and the link file common an inode number, stating that they are the same file, and the soft link original file and the linked file has different inode number, indicating that they are two different files; the soft link in the file attribute explicitly writes out the link file, and the hard link is not written. Because in essence the hard link file and the original file is completely equal, the number of links is not the same, the number of links to the soft link does not increase, the file size is not the same, the hard link file display size is the same as the original file, which is emphasized, because it is the same, and here Soft link display size and the original file is different File1 size is 48B, and File1soft is 5B, 5 of which is actually the size of "file1".

In short, creating a soft link is the creation of a new file. When accessing a linked file, the system will find that he is a link file that reads the link file to find the file that is actually being accessed.
To establish a soft link between different systems, to establish a link to the directory, here is not an example, the reader can try themselves, I am also in the practice of learning.
Of course, soft links also have the disadvantage of hard links, because the link file contains the path information of the original file, so when the original file from one directory to another directory, and then access the linked file, the system can not find the ~ ~, and hard links do not have this flaw, you want to move how to move (hehe) And it wants the system to allocate additional space for creating a new index node and saving the path to the original file.

Add: Symlink can be used to view linked files, you can use the man symlink to learn.

Third, delete the link

There is a delete when created

RM-RF Symbolic_nameAttention, not RM-RF symbolic_name/.

[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 0 07-14 14:17 file1
1491140 lrwxrwxrwx 1 root root 5 07-14 14:24 File1soft-file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
[Email protected] test]# RM-RF File1soft
[Email protected] test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 0 07-14 14:17 file1
1491139-rw-r–r–2 root root 0 07-14 14:17 file2
1491139-rw-r–r–2 root root 0 07-14 14:17 File2hand
[Email protected] test]#

————————————
The difference between a Linux soft connection and a hard link:
————————————

4 points Different:
(1) Soft connection can cross file system, hard connection is not possible.
The practice is to use shared files to connect aa.txt text documents under Windows to Linux under the/root directory BB,CC. Ln-s Aa.txt
/root/bb connection succeeded. ln aa.txt/root/bb failed.

(2) Questions about the I node. Hard connections No matter how many, are pointing to the same I node, will increase the number of node connections, as long as the node is not 0 connections, the file has been there, whether you delete the

The source file or the connected file. As long as there is one exists, the file exists (in fact, it does not divide the source file connection files, because they point to the same I node). When you modify the source file or the connection file

At any one time, the other files will be modified synchronously. Soft links do not use the I node number directly as a file pointer, but instead use the file path masterpiece as a pointer. So deleting a connection file has no effect on the source file, but

is to delete the source file, the connection file will not find the file to point to. The soft link has its own inode, and there is a small space on the disk to store the path name.

(3) A soft connection can be connected to a file name that does not exist.

(4) The soft connection can be connected to the directory.

Note: I node: it is a data structure inside UNIX that describes the characteristics of a file. We usually call the I node the file index node (information node). I node contains important information about most of the files, including the file data block

The address on the disk. Each I node has its own sign number, which we call the file sequence number. I node contains information 1. file type 2. File is the main relationship 3. Access to files 4. The time of the file is truncated.

Reference: http://www.cnblogs.com/TinyMing/p/5830852.html

Create and delete soft and hard links under Linux

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.