Hard links and soft links for Linux

Source: Internet
Author: User

Hard links and soft links

There are two types of Linux links:

? Called Hard links,

? The other is called Symbolic links (symbolic link).

By default, the LN command creates a hard link, Ln-s creates a soft connection

"Hard Connect"

A hard connection is a connection that is made through an index node.

In a Linux file system, a file saved in a disk partition, regardless of the type, assigns a number to it, called the index node number (Inode index).

Multiple filenames point to the same index node, which is usually a hard connection.

The purpose of a hard connection is to allow a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" of the function. Because deleting only one connection does not affect the index node itself and other connections, the connection to the file's data block and directory is released only if the last connection is deleted. That is, the condition that the file is actually deleted is that all the hard connection files associated with it are deleted.

Create a hard link

ln command

Syntax format:

? ln source file Destination file (the destination file may not exist and a file with the same name will be created by default when creating the link)

Example:

[[email protected] ~]# mkdir anuo[[email protected] ~]# ln anuo/ /tmp/ln: "/tmp/": 不允许将硬链接指向目录[[email protected] ~]# touch anuo.txt[[email protected] ~]# ln anuo.txt /tmp/    --创建链接到/tmp/目录下[[email protected] ~]# ll-rw-r--r--  2 root root     0 5月  13 17:54 anuo.txt[[email protected] ~]# ll /tmp/-rw-r--r--  1 root root     12 5月  13 17:57 anuo.txt[[email protected] ~]# cat /tmp/anuo.txt    [[email protected] ~]# echo "123456" >> /tmp/anuo.txt   --往链接文件里追加内容[[email protected] ~]# cat anuo.txt     --查看源文件也增加了一样的内容123456[[email protected] ~]# echo "abcd" >> anuo.txt  --给源文件追加内容[[email protected] ~]# cat /tmp/anuo.txt    --链接文件也一样增加相同的内容123456abcd[[email protected] ~]# \rm anuo.txt -f  --删除源文件[[email protected] ~]# cat /tmp/anuo.txt    --链接文件不变123456abcd

Summary:

Advantages: The hard link is equivalent to the file to make a backup, but also real-time backup to each other, the source file changes linked files are also changed, the source files are deleted, does not affect the normal use of linked files

Cons: Hard links are not available for directories and cannot be implemented across partitions

"Soft Connect"

Another connection is called a symbolic connection (symbolic link), also known as a soft connection. A soft-link file has a shortcut similar to Windows. It's actually a special file. In a symbolic connection, a file is actually a text file that contains location information for another file.

Creating a soft connection

Syntax: ln-s source file Destination file

Example:

Catalog test:

  [[email protected] ~]# mkdir anuo[[email protected] ~]# ln-s anuo/abc[[email protected] ~]# ll total dosage 4lrwxrwxrwx 1 root root 5 May 19:39 abc-anuo/drwxr-xr-x 2 root root 4096 May 19:36 anuo[[email p Rotected] ~]# touch anuo/{a. C}.txt--Create a file in source directory [[email protected] ~]# CD abc/--The link directory will also have [[email protected] abc]# lsa.txt b.txt c.txt  [[email protected] ~]# Touch abc/{1..5}.txt--Create a file in the link directory [[email protected] ~]# ll abc/total usage 0-rw-r--r--1 root Root 0 May 19:42 1.txt-rw-r--r--1 root root 0 May 19:42 2.txt ... [[email protected] ~]# ll anuo/--see the source directory also has the total amount 0-rw-r--r--1 root root 0 May 19:42 1.txt-rw-r--r--1 root Roo T 0 May 19:42 2.txt ... [[email protected] ~]# \rm anuo/-rf--Delete source directory [[email protected] ~]# CD ABC-BASH:CD:ABC: No that file or directory [[EM Ail protected] ~]# LL--you can see that the link directory is still there, but the total amount of usage 0lrwxrwxrwx 1 root root 5 May 19:39 ABC-anuo/ 

File test:

[[email protected] ~]# touch anuo.txt[[email protected] ~]# ln-s anuo.txt abc.txt[[email protected] ~]# ll total dosage 4lrwxrwxrwx 1 root root 5 May 19:51 abc-ANUO/LRWXRWXRWX 1 root root 8 May 19:55 abc.txt-Anuo . txtdrwxr-xr-x 2 root root 4096 May 19:51 anuo-rw-r--r--1 root root 0 May 19:55 anuo.txt[[email protected] ~]# echo "123456" >>anuo.txt [[email protected] ~]# cat abc.txt 123456[[email protected] ~]# echo "abcd12 3 ">>abc.txt [[email protected] ~]# cat anuo.txt 123456abcd123[[email protected] ~]# \rm abc.txt--delete  link file [[email protected] ~]# ll total dosage 8lrwxrwxrwx 1 root root 5 May 19:51 ABC--ANUO/DRWXR-XR-X 2 root root 4096 May 19:51 anuo-rw-r--r--1 root root 15 May 19:56 anuo.txt[[email protected] ~]# cat anuo.txt-source file still exists no Impact 123456abcd123[[email protected] ~]# ln-s anuo.txt abc.txt--Re-create link [[email protected] ~]# cat Abc.txt 123456a Bcd123[[email protectEd] ~]# \rm anuo.txt--Delete source file [[email protected] ~]# ll total dosage 4lrwxrwxrwx 1 root root 5 May 19:51 ABC-Anuo /LRWXRWXRWX 1 root root 8 May 20:01 Abc.txt-anuo.txtdrwxr-xr-x 2 root root 4096 May 19:51 anuo[[email  Protected] ~]# Cat abc.txt--link file also fails cat:abc.txt: no file or directory

Source file is deleted, link file is invalid

Can be created for a directory

Can be created across partitions

Soft Links: Equivalent to shortcuts in Windows

Hard links and soft links for Linux

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.