The difference between a Linux soft link and a hard link

Source: Internet
Author: User

Tags: feature hard link soft link symbolic link

One of the most important features of the Linux file system is its file link. A link is a reference to a file, which allows the file to be seen in multiple places in the file system. However, in Linux, links can be treated as if they were the original files. Links can be executed, edited, and accessed in the same way as regular files. For other applications in the system, the link is the original file that it corresponds to. When you edit a file through a link, you are actually editing the original file. Links are not replicas, and Linux has two types of links: hard links and Symbolic links (soft links).

Hard links can only refer to files in the same file system. It refers to a file's physical index (also known as an inode) in the file system. When you move or delete the original file, the hard link is not broken because it refers to the physical data of the file and not the location of the file in the file structure. A hard-linked file does not require the user to have access to the original file, nor does it show the location of the original file, which can help secure the file. If you delete a file with a corresponding hard link, the file will remain until all references to it are deleted.

Another kind of link is called symbolic link (symbolic link), also called soft link. A soft-link file has a shortcut similar to Windows. It's actually a special file. In a soft link, the file is actually a text file, which contains the location of another file, if you delete the soft link file, it is no need for this link, and the original existing entity original file does not have any relationship, but delete the original file, the corresponding soft connection is not available.

Create a hard link: ln filename "linkname"

Create a soft link: ln-s filename "linkname"

[[email protected] opt]# touch aa      #创建文件aa [[email  protected] opt]# ln aa bb      #创建aa的硬链接bb [[email protected]  opt]# ln -s aa cc     #创建aa的软链接cc [[email protected] opt ]# ll -i         #查看结果及inode值总用量  02490370 - Rw-r--r--.  2 root root 0 7 Month   30 04:36 aa2490370 -rw-r--r--.  2 root root 0 7 Month   30 04:36 bb2490371 lrwxrwxrwx. 1  root root 2 7 month   30 04:36 cc -> aa[[email protected]  OPT]# LN&NBSP, .... /opt/aa /boot/dd      #跨文件系统创建aa的硬链接dd, error message ln:  Create a hard link "/boot/dd"  =>   ". /OPT/AA ":  Invalid cross device connection [[email protected] opt]# ln -s .. /opt/aa /boot/ee     #跨文件系统创建aa的软链接ee, no error message [[email protected] opt]# ll -i /boot/        #查看结果及inode值   39 lrwxrwxrwx. 1 root root         9 7 month   30 04:37 ee ->  /opt/aa[[email protected] opt]# ln ff gg      # Create a hard link for non-existent FF GG, there is an error message ln:  is accessing "FF":  does not have that file or directory [[email protected] opt]# ln -s  ff hh      #为不存在的ff创建软链接hh, no error message [[email protected] opt]# ll - i       #查看结果及inode值总用量  02490370 -rw-r--r--.  2 root root  0 7 month   30 04:36 aa2490370 -rw-r--r--.  2 root root 0  7 Month   30 04:36 bb2490371 lrwxrwxrwx. 1 root root 2 7 Month   30 04:36 cc -> aa2490373 lrwxrwxrwx. 1 root root 2 7 Month   30 04:37  hh -> ff[[email protected] opt]# echo aaaaaaaaaa > aa      #在aa文件中写入内容 [[email protected] opt]# cat aa         #查看源文件aa的内容aaaaaaaaaa [[email protected] opt]# cat bb         #查看硬链接bb的内容aaaaaaaaaa [[email protected] opt]# cat cc         #查看软链接cc的内容aaaaaaaaaa [[Email protected] opt]# rm -f aa       #删除源文件aa [[email protected] opt]# cat bb         #删除源文件aa后查看硬链接bb的内容aaaaaaaaaa [[email protected] opt]# cat cc         #删除源文件cc后查看软链接cc的内容cat: cc:  does not have that file or directory [[email protected]  opt]# ll -i         #删除源文件后查看链接结果及inode值总用量  42490370 -rw-r--r--.  1  Root root 11 7 Month   30 04:45 bb2490371 lrwxrwxrwx. 1 root  Root  2 7 Month   30 04:36 cc -> aa2490373 lrwxrwxrwx. 1  root root  2 7 Month   30 04:37 GG -> FF

From the above operation can see the hard link file with the Inode value of the source file, that is, the hard link file and the source file belong to a data block, the soft link file and the source file Inode value is not the same, that is, the soft link file is a separate text file. When the source file is deleted, the hard-link file is not affected, and the soft-link file is not valid.

Hard Link Features:

    • 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;

Soft Link Features:

    • 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, then the related soft connection is called the dead link (that is, dangling link, if the point path file is re-created, the dead link can be restored to the normal soft link);

The relationship between soft links, hard links, and source files as shown:

650) this.width=650; "src=" Http:// "title=" 5.png " alt= "Wkiol1ebqm6yynloaacnxbcf-la413.png"/>

This article is from the "Linux Operational Learning path" blog, please be sure to keep this source

The difference between a Linux soft link and a hard link

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: