What is the difference between soft links and hard links in Linux?

Source: Internet
Author: User

The following is my personal learning Linux/unix environment programming summed up the difference between soft links and hard links, for reference only

Soft links can be cross file systems, hard links are not available.

Hard links and source files share an inode, which means that it is essentially the same file, and the soft links have their own inode with the source file, indicating that they are independent of different files.

When you look at the properties of the file (ls-l), the link file is indicated by the soft link, and the hard link is not marked because the hard link and the source file are equivalent in nature.

The number of linked pointers in the Inode node is different, and when you create a hard link, the number of linked pointers in the Inode node of the source file is added to one, and the soft link does not.

The file size is different, the hard link file is the same size as the source file, and the size of the soft link is the length of the source file file name.
A soft link file contains path information for the source file, so a soft link to the file when the source file is moved will not find the source file. and hard links can be moved at will.

Soft links Create a soft link for a directory, and only the root user can create a hard link for the catalog file.
You can create soft links for files that do not exist, but you cannot create hard links.

Example

1: Soft Link

Command: Ln–s/etc/inittab/test/inittab.soft

View: Ls–l/etc/initab/test/inittab.soft

Found a soft connection similar to Windows shortcut,-> represents the address of a real file

2: Hard Link

Command: Ln/etc/inittab/test/inittab.hard

View: Ls-l/etc/inittab/test/inittab.hard

The hard links are found to be exactly the same size and creation date as the real file, similar to copy, synchronized updates.

3: The difference between hard link and copy:

The found copy is different from the date the actual file was created.

4: If you want to replicate the same date, you can pass the command: CP–P/ETC/INITTAB/TEST/INITTAB2

5: Sync Update for hard links

A soft connection is similar to a shortcut that accesses a source file, all of which must be synchronized with the source file.

Hard links are similar to copies, but have the ability to synchronize updates, which we can verify.

First create a file link in the/test directory

(1) The soft connection file and the hard link file of the link are worn separately, Link.soft Link.hard

(2) Write data to the source file link

(3) View the contents of three files

Find hard link files synchronized updates with source files

6: Effects on hard links and soft connections after you delete the source files

A soft connection is similar to a shortcut, and it is not valid when the source file does not exist.

A hard link is a copy and still exists.

7: Why hard links can be updated synchronously?

Also creates a file link and its soft links and hard link files,

With the command ls–i, the hard link file and the source file are found to be the same as the I node number, and the kernel is based on the file's I node to identify the file, the same I node of the two files, all considered a file, all simultaneously write and modify the data.

8: Requirements of the hard link

Hard links cannot cross the file system, that is, you cannot cross partitions, similar to the hard links in C that cannot be placed in D on Windows. and soft links can be placed in any file system.

All right, here we go. About soft links and hard links I guess you know a little bit about it.

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.