Linux hard links, the creation of soft links, and the difference between the two

Source: Internet
Author: User

One, hard links

Number of links is the number of names:

ln (link) hard links, multiple identical inode files (same partition), multiple hard-link files exist on the hard disk only occupy one file capacity.

The hard links created are equal, the inode is the same, the deletion of one is not affected, and the other files can still be used.

The nature of hard links: Create multiple names for a file.

Create multiple hard links to see how the number of links changes (+1), the number of links is a few file names.

In the same partition, the same inode number must be a file.

The original file

[Email protected] testdir]# ll-iman.txt15-rw-r--r--. 1 root root 15978 8 2008man.txt


Create first hard link

[Email protected] testdir]# lnman.txt test/f11[[email protected] testdir]# ll-iman.txt test/f11 15-rw-r--r--. 2 root root15978 8 man.txt15-rw-r--r--. 2 Root root15978 8 test/f11


Create a second hard link

[Email protected] testdir]# lnman.txt testdir/f22[[email protected] testdir]# ll-iman.txt testdir/f22 test/f11 15-rw-r- -R--. 3 root root15978 8 man.txt15-rw-r--r--. 3 root root15978 8 testdir/f2215-rw-r--r--. 3 Root root15978 8 test/f11


When you create multiple hard links to a file, all files have the same inode, permissions, size, and time attributes.

[Email protected] testdir]# echo "AAAAAAAAAAAAAAAAAAA" >man.txt[[email protected] testdir]# ll-itestdir/f22 test/ F11 Man.txt Test/f15-rw-r--r--. 4 root root 8Jul 09:49 man.txt15-rw-r--r--. 4 root root 8Jul 09:49 testdir/f2215-rw-r--r--. 4 root root 8Jul 09:49 test/f15-rw-r--r--. 4 root root 8Jul 09:49 test/f11


When writing data to a file, the properties of other files are changed as well.

[[email protected] testdir]# lnman.txt/roo/aln:creating hard link '/roo/a ' = ' man.txt ': No such file or directory


Hard links cannot be created across partitions, across devices

[Email protected] testdir]# lnhelp/hln: ' help/': Hard link notallowed for directory


Hard links cannot be for directories

[[Email protected] testdir]# ll -iman.txt man131 -rw-r--r--.  6 root  root3256 aug  1 16:54 man131 -rw-r--r--.  6 root root3256  aug  1 16:54 man.txt[[email protected] testdir]# rm -fman.txt  [[email protected] testdir]# tailman  -Z, --ditroff               use groff and force it  to produceditroff   -?,  --help                  give this help list       --usage                 give a short usage message  -V, --version               print program version    Mandatory or  Optionalarguments to long options are also mandatory or optionalfor  any corresponding shortoptions. report bugs [email protected]


When the original file is deleted, the linked file can still be viewed

Second, soft (soft) Link:

Soft links are equivalent to Windows shortcuts

[Email protected] testdir]# ln-sman.txt man[[email protected] testdir]# ls-li man.txt man12 lrwxrwxrwx. 1 root root 7Jul 10:04 man--Man.txt15-rw-r--r--. 4 root root 8Jul 09:49 man.txt


Create a soft link file, the link file will be identified in green font and point to the original file, through observation can find two files of different inode number

Create a soft link file for the original file

[[email protected] testdir]# ln –sman.txt /roo/ man.txt[[email protected] testdir]# ll/root/man.txtlrwxrwxrwx. 1 root root   7 Aug 1 16:55  /root/man.txt ->man.txt[[email protected]  Testdir]# ln –s. /.. /testdir/man.txt /root/man1[[email protected] testdir]# ll/root/man1lrwxrwxrwx. 1  ROOT ROOT  9 AUG 1 16:56 /ROOT/MAN1 ->&NBSP, .... /.. /testdir/man.txt [[email protected] testdir]# ln -s/testdir/ /root/test1[[email  protected] testdir]# ll -d/testdir/ /root/test1lrwxrwxrwx. 1 root  Root  9 aug 1 17:02 /root/test1 -> /testdir/drwxr-xr-x. 3  root root 34aug  1 16:53 /testdir/


Through the above example can be found that soft links can be created for the directory, cross-partition, and the creation of the time to pay attention to the problem of the path, if the path is wrong, the link file will save the displayed

Soft links need to be aware of absolute and relative paths relative to the path of a soft link rather than the path of the current directory (pointing to the location relative to the current working directory or directory)

[[email protected] testdir]# ln -s/testdir/man  /root/111111[[email protected] testdir]# ll -i/testdir/man /root/111111105103873  lrwxrwxrwx. 1 rootroot   12 aug  1 17:16 /root/111111  -> /testdir/man      131 -rw-r--r--.  5 root  root 3256 aug  1 16:54 /testdir/man[[email protected] testdir]#  rm -f/testdir/man [[email protected] testdir]# ll/testdir/man /root/111111  Ls: cannot access/testdir/man: no such file or directorylrwxrwxrwx. 1  root root 12Aug  1 17:16 /root/111111 ->/testdir/man[[email  Protected] testdir]# cat/root/111111 cat: /root/111111: no suchfile or  directory 


When the original file is deleted, the created soft link file will not be accessible

Third, the difference between soft and hard links:

The difference between a soft link and a hard link is that the soft link is invalidated after the original file is deleted, but the hard link is still available, and the soft link works for the file or directory, but the hard link is only available for the file, not a hard link to the directory, and the soft link and the original file can be in different file systems. However, the hard link must be within the same file system as the original file (such as a Linux partition).


This article is from the "Linux on the Road" blog, be sure to keep this source http://dreamlinuxc.blog.51cto.com/5733156/1833082

Linux hard links, the creation of soft links, and the difference between the two

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.