Soft and hard links in Linux that's the thing.

Source: Internet
Author: User
Tags parent directory

Soft connections and Hard links

The previous article has introduced to you the Linux system file directory structure, see http://vinsent.blog.51cto.com/13116656/1959522. Based on the Times, this blog introduces you to Soft links (also known as symbolic links) and hard link in Linux file systems, including the concept of soft and hard connections, the difference between soft and hard links, and how to create soft and hard links. Some of the concepts in https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/are referenced in this article, so please tell me if there is infringement. Delete now.

One, relative path and absolute path

Before introducing the soft and hard link, we introduce the relative path and the absolute path, we make the directory switch in Linux, and the large age library files brought by the system are implemented by the symbolic link files formed by relative paths. Most of our operations are in contact with hard links, like switching directories (cd/etc/sysconfig/network-scripts/), mounting Files (mount/dev/sdxx/mnt/), and so on.

1. Absolute path

The absolute path is "path Absolute", stating that there is only one path to reach our "destination". An absolute path begins with a root "/" (such as/etc/rc.d/inittab), so long as a path is rooted from the root, there is no doubt that it must be an absolute path. For example:

[Email protected] ~]#[[email protected] ~] #pwd # View current directory/root[[email protected] ~] #cd/etc/rc.d/init.d/ # Enter/etc/rc.d/init.d/directory with absolute path [[email protected] INIT.D] #lsfunctions netconsole network readme[[email protected] Init . d] #pwd # PWD View current directory as/etc/rc.d/init.d//etc/rc.d/init.d[[email protected] INIT.D]#

2. Relative path

Relative path: As the name implies, the path is relative, relative to a specified directory or currently in the directory. The relative path is with ". "or" ... " Began, ". "Indicates the location of the user's current operation, while". "indicates the parent directory; in the path,". "represents the directory in which the user is currently located, and the". "Parent directory, to put". "and". "As a catalogue. For example:

[ [email protected] init.d ]#[ [email  protected] init.d ] #pwd       #  in hard links we entered the/etc/rc.d/. INIT.D directory, PWD view the current directory/etc/rc.d/init.d[ [email protected] init.d ] #cd  ..     #&NBSP, .... Representative of the Superior directory, cd    Enter into the/ETC/RC.D directory [ [email protected] rc.d ] #pwd/etc/rc.d[ [email protected]  rc.d ] #cd         [ [email protected] etc ] #pwd/etc[ [email  protected] etc ] #cd   /boot     #  switch from the current directory to the boot directory [ [email protected] boot ]#[  [email protected] boot ] #pwd          #  pwd  viewing the current directory as/boot/boot[ [email protected] boot ]# 

Second, soft connection and hard link

1. Hard Links

A hard link is a link that is made through an index node. In Linux, multiple files pointing to the same index node are allowed, and links like this are hard links. Hard links can only be linked between files in the same file system and cannot be created on the directory. If you delete the source file for the hard link, the hard-link file still exists, and the original content is saved, which can prevent the file from being mistakenly deleted because of the wrong operation. Because a hard link is a file with the same inode number with only a different file name, deleting a hard-link file does not affect other files that have the same inode number; plots hard links:

2, soft links (also known as symbolic links) and hard links, the contents of the file user data block is the path name of another file point. Soft link is a normal file, but the content of the data block is a bit special. Soft links can be created on files or directories. Soft links are mainly used in the following two aspects: first, convenient management, for example, a complex path can be linked to a simple path to the user access, on the other hand, to resolve the file system disk space shortage. For example, a file system has run out of space, but now you have to create a new directory under the file system and store a large number of files, you can link another file system with more remaining space in the file system, which can be a good solution to the problem of space shortage. Deleting a soft link does not affect the file being pointed to, but if the original file being pointed to is deleted, the associated soft connection becomes a dead link. You can create symbolic links by using the-s option of the LN command. is the working process of a soft connection:

3. The similarities and differences between soft connection and hard link

we can feel the difference between the soft connection and the hard link concept, and summarize the similarities and differences below. because a hard link is a file with the same inode number with only a different file name, hard links have the following characteristics:

The file has the same inode and data block;

Only files that already exist can be created;

Cannot cross file system for hard link creation;

The directory cannot be created, only the file can be created;

Deleting a hard-link file does not affect other files that have the same inode number;

A soft link differs from a hard link in that the file is a soft connection if the contents of the file's user data block are pointing to the path name of another file. Soft link is a normal file, but the content of the data block is a bit special. Soft links have their own inode numbers and user data blocks so the creation and use of soft links does not have many restrictions similar to hard links:

Soft links have their own file attributes and permissions, etc.;

You can create a soft link to a nonexistent file or directory;

Soft link can cross file system;

Soft links can be created on files or directories;

When you create a soft link, the link count i_nlink not increase;

Deleting a soft link does not affect the file being pointed to, but if the original file being pointed to is deleted, the associated soft connection is called a dead link (that is, dangling link, if it is re-created by pointing to the path file, the dead link can revert to the normal soft link).

4. Create soft links and hard links

introduced the concept of soft and hard links and their similarities and differences; take a step-by-step to create a soft, hard link, from the experiment to understand the difference between soft and hard links. Hard links are implemented using "ln", and soft joins are implemented using "Ln-s".

#------------Experimental Environment Preparation-----------#[ [email protected] ~ ]#[ [email protected]  ~ ] #rm  -rf /app/*     #  in order not to affect the experiment, we emptied the/app/directory [ [email  protected] ~ ] #cd  /app              #  switch to/app directory [ [email protected] app ] #ls [ [email protected] app  ] #pwd/app[ [email protected] app ] #mkdir  -p /app/{a/{x/{m,n/k},y},b,c/{q,w /{e/yy,r}},d}  #  creating several directories [ [email protected] app ] #tree  /app          # tree View the/app tree structure/app├── a│   ├── x│    │   ├── m│   │   └── n│    │       └── k│   └── y├── b├── c│    ├── q│   └── w│       ├── e│        │   └── yy│       └── r└──  d14 directories, 0 files[ [email protected] app ]#[ [email  protected] app ] #cd  /app/a/x/n/k       #  Enter to/app/a/x/n/ k  Create two files [ [email protected] k ] #touch  1.txt[ [email protected] k  ] #touch  2.txt[ [email protected] k ] #cd  /app/c/w/e/yy          #  go to/app/c/w/e/yy  Create two files [ [email protected] yy &NBSP,] #touch  11.txt[ [email protected] yy ] #touch  22.txt#------------ Create a hard link--------------#[ [email protected] ~ ] #ln  /app/c/w/e/yy/11.txt /app/a/x/n/k /1bak.txt  #  Create a hard link [ [email protected] yy ] #lltotal  0-rw-r--r--.  2 root root 0  Aug 28 21:43 11.txt   #  node count increased to 2-rw-r--r--.  1 root root  0 aug 28 21:43 22.txt[ [email protected] yy ] #cd  /app/a/x/n /K/[ [EMAIL PROTECTED] K&NBSP,] #lltotal  0-rw-r--r--.  2 root root 0  Aug 28 21:43 1bak.txt  #  node count increased to 2  description hard link creation successful-rw-r--r--.  1  Root root 0 aug 28 21:42 1.txt-rw-r--r--.  1 root root 0  aug 28 21:42 2.txt[ [email protected] k ] #echo  123634124  > 1bak.txt        #  Change file  1bak.txt  content [ [ email protected] k ] #cat  1bak.txt123634124[ [email protected] k ] #rm  -rf 1bak.txt               #  delete the linked file [ [ email protected] k ] #cd  /app/c/w/e/yy               #  switch to/app/c/w/e/yy directory [ [email protected] yy ] #ll                              #  view the number of nodes reduced total 4-rw-r--r--.  1  Root root 7 aug 28 21:54 11.txt-rw-r--r--.  1 root root 0  aug 28 21:43 22.txt[ [email protected] yy ] #cat  11.txt                    #   Viewing the contents of the source file has not changed 123124 [ [email protected] app ] #ln  abc /etc/abc_bak          #  creating hard links across partitions, prompting error ln: failed to create hard link  '/ETC/ABC _bak '  =>  ' abc ':  invalid cross-device link#------------Creating a soft connection--------------#[ [ email protected] init.d ] #cd  /etc/rc.d/rc3.d/     #  Switch to/etc/ rc.d/rc3.d/ [ [email protected] rc3.d ] #ll                         #  A soft connection behaves as one directory pointing to another directory total 0lrwxrwxrwx. 1 root root 20 aug 14 11:09  K50NETCONSOLE ->&NBSP, .... /init.d/netconsolelrwxrwxrwx. 1 root root 17 aug 14 11:09 s10network  ->&NBSP, .... /init.d/network[ [email protected] yy ] #ln  -s   /.. /.. /.. /C/W/E/YY/11.TXT &NBSP, .... /.. /.. /.. /a/x/n/k/11bak.txt   #  creating a soft connection [ [eMAIL PROTECTED] YY&NBSP,] #lltotal  4-rw-r--r--.  1 root root 38 aug  28 22:17 11.txt-rw-r--r--.  1 root root  0 aug 28 21:43  22.txt[ [email protected] yy ] #cd  /app/a/x/n/k/[ [email protected]  k ] #ll             #  view soft Connect total  0lrwxrwxrwx. 1 root root 27 Aug 28 22:18 11bak.txt -> &NBSP, .... /.. /.. /.. /c/w/e/yy/11.txt-rw-r--r--.  1 root root  0 aug 28 21:42 1. Txt-rw-r--r--.  1 root root  0 aug 28 21:42 2.txt[ [email  protected] k ]# [ [email protected] k ] #cat  11bak.txt           #  View File Contents 1sdfsaldfhaskdhjashdkfjkasdfk23124[ [ Email protected] k ] #echo  82734 > 11bak.txt     #  Modify the contents of the file [ [email  protected] k ] #cat  11bak.txt 82734[ [email protected] k ] #cd  /app/c/w/e/yy/          #  switch back to source directory [ [email  protected] yy ] #ll                total 4-rw-r--r--.  1 root root 6 aug 28 22:21 11. Txt-rw-r--r--.  1 root root 0 Aug 28 21:43 22.txt[ [email  protected] yy ] #cat  11.txt        #  View the contents of the original file, The discovery was also modified; indicates that the soft connection is the same file 82734[ [email protected] yy ] #rm  -rf 11.txt      #  Delete original file [ [email protected] yy ] #cd  -               #  switch back to the last directory/app/a/x/n/k[ [email protected] k ] #ll                   #  See the link file and find it turned red; total  0lrwxrwxrwx. 1 root root 27 Aug 28 22:18 11bak.txt -> &NBSP, .... /.. /.. /.. /c/w/e/yy/11.txt[ [email protected] k ] #cat  11bak.txt       #  viewing files   contents, prompting for error cat: 11bak.txt: no such file or directory[ [ email protected] app ] #ln  -s  /APP/ABC&NBSP, .... /root/abc_bak  #  created a soft connection in different partitions, successfully [ [EMAIL PROTECTED] APP ] #cd  /root[  [email protected] ~ ] #ll               #  different partitions Create a soft connection successfully; Description can be lrwxrwxrwx. 1 root root     across partitions   10 aug 28 22:28 abc_baK ->&NBSP, .... /app/abc

Additional notes:

    1. Of course, soft-linked user data can also be an absolute path path. If it is not the same root, the path of the original file when the soft link is created is better with absolute path. This is when a soft link created with a relative path is moved and the soft link file becomes a dead link, because the linked data block also points to a relative path. If the link file and the original file under the same root, then the relative path to achieve better, he can perfect the overall migration, without destroying the structure, the use of absolute path will not find the original file. Therefore, when creating a soft connection, the recommendations are subject to availability.

    2. In a soft-connect relative path, the destination file is relative to the current directory or the directory in which the original file resides, whereas the path to the original file is relative to the path of the target linked file. Perhaps you will find it difficult to understand, the following illustrated way to help you:




This article is from the "vinsent" blog, make sure to keep this source http://vinsent.blog.51cto.com/13116656/1960302

Soft and hard links in Linux that's the thing.

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.