Soft links and hard links under Linux

Source: Internet
Author: User

Start by talking about which command in Linux you use to create a linked file, and then we'll talk about the difference between soft links and hard links.


Use LN command to create a linked file for a file

Usage: LN [-s-v] src dest

Create a hard connection file: LN Original file hard Connect file

hard links to the source file Inode number is the same, use Stat command to view the metadata information for a file.

Create a soft connection file: ln-s Original File Soft Connection file

Plus - S represents creating a soft connection (symbolic link)

Note: When you create a connection file, you specify the source file, The path to the original file can be either an absolute or a relative path, but note that if the source file path is written relative to the path, the relative path is not relative to the current directory, but relative to the location of the connection file, so If you are creating a soft connection to a file, it is important to note that this "relative path" concept, which is not easy to turn around, can be seen in the following example

TestDir the directory has a T1 directory, T 1 the directory has a AA file

[[Email protected]]# TREE/TESTDIR//TESTDIR/└──T1└──AA


The directory is now in T 1 in the catalog

[Email protected] t1]# PWD/TESTDIR/T1

if we want to target the current directory AA file creates a soft connection, and you want the soft-connect file to be placed in the TestDir directory, you can use the following command

[Email protected] t1]# ln-s t1/aa /testdir/aasoftlink


As you can see, the source file uses a relative path, which is relative to the /testdir/aasoftlink this file, not relative to the working directory where the command was run

re-use Tree command to view the directory structure, the soft connection has been generated to the specified location, so it is more clear that the source file relative path should be written.

[Email protected]]# tree/testdir//testdir/├──aasoftlink-> T1/AA└──T1└──AA


View the soft connection that you just created, pointing to the relative path below the AA Original File

[[Email protected]]# ll/testdir/aasoftlink

lrwxrwxrwx. 1 rootroot 5 Jul 16:27 /testdir/aasoftlink- t1/aa


Hard Links: If two file paths point to the same Inode , then they are called hard links between each other

when a file (non-catalog file) has a hard link number equal to 1 , it is only possible that it is actually deleted.


when a directoryAis created, the number of connection references by default is2, because in this directoryA, contains a "."file,represents the current directory, which"."is the hard link to the current directory, which"."of theInodenumber withAof the CatalogInodeso that the minimum number of hard links per directory is2, if theAThe catalog also creates abdirectories andCdirectory, thenAthe number of links will become4, this is becauseA/ band theA/ CBoth of these directories contain".."This file,and".."represents the top-level directory of the current directory, relative tobdirectories andCdirectory, the top-level directory of the current directory isAdirectory, soA/ band theA/ Cin both directories, the".."of theInodeNumber and directoryAof theInodethe number is the same , the root directory is more special because the root directory"."and the".."are hard links to the root directory.

Hard Links:

1 Hard Links Inode number and source file Inode Same number

2 can only be created on files and cannot be applied to directories

3 cannot span file systems and cannot span partitions. Because the inode number is the unique identifier of the file under a partition, the inode number of the hard link and the source file inode No hard links can be created across partitions

4 creating a hard link increases the number of times a file is linked



Soft Connect: If the pathathere's am. txtfile, the system found it.M.txtof theInodenumber that was originally passedInodeYou should find the block number where the information is stored, and then find the information in the block, but the information in the block does not hold the real information, but instead holds the path name of the other file, if the data block is stored inbPath ofN.txt, then, the system is looking againN.txtof theInodeinformation, and then find the corresponding block number to access the data file. Symbolic Links General Permissions are777, because the final permission is not determined by it, it only replicates to let the user find this link.

Soft connections, symbolic connections:

1 can be applied to a table of contents

2 can cross file system

3 does not increase the number of links to linked files

4 The size of the specified path contains the number of characters

The difference between a soft connection and a hard connection looks

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/85/3F/wKioL1eeG4HjbP61AAAV8AxSD7k006.jpg "title=" Link.jpg "alt=" Wkiol1eeg4hjbp61aaav8axsd7k006.jpg "/>


This article is from the "School of Thought" blog, please be sure to keep this source http://zz6547.blog.51cto.com/2571551/1832951

Soft links 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.