Create and delete soft and hard links in linux

Source: Internet
Author: User
Tags symlink

In Linux, the kernel allocates an Inode (index node) to each newly created file. Each file has a unique inode number. The file attributes are stored in the index node. when accessing the file, the index node is copied to the inner to implement fast file access. A link is a way to establish a connection between a shared file and several directory items of the users who access it. Linux contains two types of links: Hard Link and Soft Link. Soft link is also called Symbolic Link ). Symbolic connection is equivalent to a shortcut in Windows. I. Hard link hard link is a pointer pointing to the file index node, and the system does not re-allocate inode for it. You can use the ln command to create a hard link. Syntax: ln [options] existingfile newfile (sexistingfile: file of the link to be created, newfile is a newly created link file) when ln [options] existingfile-list directory-f is created, delete the same file name. -I ask before deletion. ln-s abc cde establishes a soft connection of abc ln abc cde to establish a hard connection of abc. Usage: Create a hard link for "existingfile" and the file name is "newfile ". Type 2: Create a hard link with the same name for all files contained in "existingfile-list" in the "directory" directory. Commonly used [options]-f creates a link regardless of whether "newfile" exists or not. -N if "newfile" already exists, no link is created. The following are some examples: [root@rekfan.com test] # ls-il total 01491138-rw-r-1 root 48 07-14 file11491139-rw-r-2 root 0 07-14 file2 [root@rekfan.com test] # ln file2 file2hand [root@rekfan.com test] # ls-il total 01491138-rw-r-1 root 48 07-14 file11491139-rw-r -r-2 root 0 07-14 file21491139-rw-r-2 root 0 07-14 file2hand [root@rekfan.com test] # note that before creating a link, fi The number of links displayed in le1 is 1. After a link is created, the number of links in file1 and file1hard is changed to 2. file1 and file1hard have the same inode number as file1 and file1hard. The result of ln command is as follows: file1 and file1hard are two names of the same file. They have the same index node number and file attributes and create a hard link to file1, creates a new pointer for the file index node of file1 in the current directory. You can delete any of them, such as rm file2. Only one pointer is deleted at a time, and the number of links is reduced by one. When the number of links is reduced to 0, the kernel will delete the file content from the disk. You can also create hard links to files in different directories but in the same file system. Set file1 and file2 to the/home/root/dir1 directory. Run the following command to create a hard link for file2 in/home/root. In ln file2/home/root/file2hard, all files in the dir1 directory are stored, create a hard link in the directory dir2 # mkdir dir2 # ln/home/root/dir1/*/home/root/dir2 if ln-f existingfile newfile is used, if newfile already exists, no matter what the original newfile is, newfile becomes a hard-linked file of exisitngfile with the write permission of the current user. Although hard links save space, they are also the traditional way to integrate file systems in Linux systems, but there are some shortcomings: (1) You cannot establish links between files in different file systems (2) only Super Users can create hard links for directories. 2. Soft links (symbolic links) soft links overcome the shortcomings of hard links and do not have any restrictions on the file system. Any user can create symbolic links pointing to directories. As a result, it is more widely used. It has more flexibility and can even link files across different machines and networks. To create a soft link, add the option-s after ln, the following example [root@rekfan.com test] # ls-il total 01491138-rw-r-1 root 48 07-14 file11491139-rw-r-2 root 0 07-14 file21491139-rw-r-2 root 0 07-14 file2hand [root@rekfan.com test] # ln-s file1 file1soft [root@rekfan.com test] # ls-il total 01491138-rw-r-1 root 48 07-14 file11491140 lrwxrwxrwx 1 root 5 07-14 file1soft-> fil E11491139-rw-r-2 root 0 07-14 file21491139-rw-r-2 root 0 07-14 file2hand the result after the above link can be we can see that soft links and hard links, the difference is not only in terms of concept, but also in terms of implementation. Difference: A Public inode number is used for hard-link files and linked files, indicating that they are the same file, while soft-link files and linked files have different inode numbers, it indicates that they are two different files. On the file attributes, the soft link clearly writes the link file, but the hard link is not written, in essence, hard-link files are completely equal to the original files. The number of links is different, and the number of soft-link links does not increase. The file size is different, the size of the hard link file is the same as that of the original file. This is emphasized because it is equivalent, and the size of the soft link is different from that of the original file, the size of file1 is 48B, while that of file1soft is 5B, where 5 is actually the size of "file1. In short, creating a soft link is to create a new file. When you access a linked file, the system will find that it is a linked file. It reads the linked file and finds the file to be accessed. Creating soft links between different systems and creating links to directories is not an example here. You can try it on your own. I am also learning it in practice. Of course, soft links also have the disadvantage that hard links do not exist. Because the link file contains the path information of the original file, when the original file is moved from one directory to another, access the link file, the system cannot find it ~~, Hard links do not have this defect, so you need to move them as much as you want (haha). In addition, the system needs to allocate additional space for creating new index nodes and saving the original file path. Supplement: You can view the link file through symlink, and you can use man symlink to learn. 3. Delete the link has been created to delete rm-rf symbolic_name note not rm-rf symbolic_name/[root@rekfan.com test] # ls-il total 01491138-rw-r-1 root 0 07-14 file11491140 lrwxrwxrwx 1 root 5 07-14 file1soft-> file11491139-rw-r-2 root 0 07-14 file21491139-rw-r- r-2 root 0 07-14 file2hand [root@rekfan.com test] # rm-rf file1soft [root@rekfan.com test] # ls-il total 01491138-rw-r-1 Root 0 07-14 file11491139-rw-r-2 root 0 07-14 file21491139-rw-r-2 root 0 07-14 file2hand [root@rekfan.com test] # ------------ linux soft connection and hard link difference: ------------ different from each other at: (1) the soft connection can be cross-file system, but the hard connection cannot. The practice is to use a shared file to connect the aa.txt text file in windows to bb, cc. ln-s aa.txt/root/bb in the/root directory in linux. Ln aa.txt/root/bb failed. (2) I node issues. No matter how many hard connections direct to the same I node, the number of connections at the node increases. As long as the number of connections at the node is not 0, the file will always exist, whether you delete the source file or the connected file. As long as one exists, the file exists (in fact, no source file is connected, because they all point to the same I node ). When you modify the source file or the connection file, other files are synchronized. Soft links do not directly use the I node number as the file pointer, but use the file path name as the pointer. Therefore, deleting a connection file does not affect the source file. However, if you delete the source file, you cannot find the file to be directed. Soft links have their own inode, and there is a small space on the disk to store the path name. (3) soft connections can be used to connect a non-existent file name. (4) soft connections can be used to connect directories. Note: I node: it is a data structure used within UNIX to describe file features. we usually call I a file index node (information node ). the I node contains most important information about the file, including the address of the file data block on the disk. each I node has its own flag number, which is called the document sequence number. I node Information 1. file Type 2. file owner relationship 3. file Access permission 4. file time cut.

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.