How to create and delete soft links and hard links under Linux

Source: Internet
Author: User
Tags symlink

In Linux, the kernel assigns an Inode (index node) to each newly created file, and each file has a unique inode number. The file attributes are stored in the index node, and when the file is accessed, the index nodes are copied to the inside to enable fast 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. Linux includes two kinds of links: Hard link (Hard link) and soft link (Soft link), soft link is also known as symbolic link (symbolic link). Symbolic connections are equivalent to shortcuts under Windows.

One, Hard link

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

When-F is established, it is deleted with the file name.
-I to ask before deleting.

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

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

Here are some examples:

[Root@rekfan.com 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
[root@rekfan.com test]# Ln file2 file2hand
[Root@rekfan.com 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
[Root@rekfan.com test]#

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

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

ln File2/home/root/file2hard
The following procedure is to create 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 newfile already exists, then no matter what file the original NewFile is, only the current user has write access to it, and NewFile becomes the hard link file for Exisitngfile.

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

Second, soft link (symbolic link)

Soft links overcome the lack of hard links, no file system restrictions, any user can create a symbolic link to the directory. So now it's more widely used, it has more flexibility, and can even link files across different machines and networks.
To create a soft link, just add the option –s to the LN, and here's an example

[Root@rekfan.com 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

[root@rekfan.com test]# ln-s file1 file1soft
[Root@rekfan.com test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 07-14 14:17 file1
1491140 lrwxrwxrwx 1 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 to soft links and hard links, the difference is not only in the concept, in the implementation is also different. Difference: Hard Link Original file & Link file common an inode number, stating that they are the same file, and that the soft link original file and link file have different inode numbers, indicating that they are two different files; the soft links on the file properties explicitly write the link file, and the hard link is not written, Because in essence the hard link file and the original file are completely equal; The number of links is different, the number of links to soft links does not increase; File size is not the same, hard link file display size is the same as the original file, which is emphasized, because it is equivalent, and here the soft link display size and the original file is different, The file1 size is 48B, and the File1soft is 5B, and the 5 is actually the size of "file1".

In short, creating a soft link creates a new file. When you access the linked file, the system finds that he is a linked file that reads the linked file and finds the file that you really want to access.
The establishment of soft links between different systems, the directory to establish links, here is not an example, the reader can try for themselves, I am also in the continuous practice of learning.
Of course, soft links also have the disadvantages of hard links, because the link file contains the original file path information, so when the original file from a directory moved to other directories, and then access to the link file, the system can not find ~ ~, and hard links do not have this flaw, you want to move on how to move (hehe) And it wants the system to allocate additional space for creating new indexing nodes and for saving the path of the original file.

Add: You can see the link file through Symlink, you can learn it with man symlink.

Third, delete the link

There is a deletion of the creation

RM-RF symbolic_name attention is not RM-RF symbolic_name/

[Root@rekfan.com test]# Ls-il
Total 0
1491138-rw-r–r–1 root root 0 07-14 14:17 file1
1491140 lrwxrwxrwx 1 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
[Root@rekfan.com test]# RM-RF File1soft
[Root@rekfan.com 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
[Root@rekfan.com 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 approach is to use shared files to connect Windows Aa.txt text documents to the Linux/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 connection no matter how many, are pointing to the same I node, will increase the number of node connections, as long as the number of connections is not 0, the file is always there, regardless of what you deleted is

The source file or the connected file. As long as there is one existence, the file exists (in fact, no source files are connected to the file, because they point to all the same I node). When you modify a source file or a connection file

At any one time, the other files will be synchronized changes. Soft links do not use the I node number directly as a file pointer, but instead use file path masterpieces as pointers. So deleting the 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. Soft links have their own inode, and there is a small piece of space on the disk to store the path name.

(3) A soft connection can connect to a nonexistent file name.

(4) 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 (the information node). The I node contains most of the important information about the file, including the file data block in the

The address on the disk. Each I node has its own flag number, which we call the file order number. I node contains information 1. file type 2. File is the main relationship 3. Access rights for files 4. The time cut of the file.

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.