On the use of linux hard links and soft links and file of ln

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/LEON1741/article/details/100136449

There is a link to the file in the file called linux system can be used to solve the shared files. The link can be divided into two types, one is a hard link (Hard Link), the other is also referred to as a soft link or symbolic link (Symbolic Link).

First, the basic concept

1, a hard link

Before you begin, first explain a concept called the inode (Inode).

In the Linux file system, save the file in the disk partition, regardless of what type, the system will assign it a number, this number is called the inode number (Inode Index), which is the file or directory in the file linux unique identification system. With this number value, can be found on the details of the file.

At the same time, Linux system also provides that can allow multiple file names while hard links to the same inode (Inode), that is. This design has the advantage that, as long as the file's inode there is more than one link, delete a link does not affect the inode itself and the other links (that is the entity of the file is not deleted), and only when the last after a link is deleted, and this time there is new data to be stored on disk, then link the data blocks and directory of the deleted files will be released, the storage space will be overwritten by new data. Thus, this mechanism can effectively prevent erroneous deletion operation.

Hard links can only be linked in the same type of file system, the file system can not cross. At the same time it can only link to the file, you can not link directories.

2, soft links

Soft links (also called symbolic links), similar to the shortcuts in the windows system, with different hard links, soft links is a normal file, just a little bit special data block contents, the contents of the file the user data block is stored in another file point to the path name of the source file can quickly locate entities soft connection points by way of this.

Soft links used to solve the problem of insufficient space, such as a file system has run out of space, but must now create a new directory in the file system and store large number of files, you can put more of the other remaining space file system directory is linked to the file system.

Soft links can span file systems and links, can simultaneously be linked to a file or directory.

3. The difference between the two

  • Stored in the form of soft link to another file path exists, hard links in the form of a copy of the file;
  • Soft links across different file systems and links, hard links can not;
  • Soft links can link to the directory, and hard links can not;
  • Soft links can be linked to a file name that does not exist, there must be a hard link source file.
  • Delete soft link does not affect the file pointed to, but if the point of the original file is deleted, the associated soft connection becomes a dead link. Delete hard links, so long as the inode number is not zero, the original file will not have any impact;

Note: Whether soft or hard links will not link the original copy of the target file completely, but only occupy a very small amount of storage space.

Second, the way to create (ln Command)

Soft links and hard links are created using the ln command, but with different parameters. Command format is as follows:

ln 参数 源文件或目录 目标文件或目录

Note: The source and target directories must be an absolute path!

parameter:

  • -i interactive mode, the user is prompted whether the file exists coverage;
  • -s soft links (symlinks);
  • -d allows super users to create hard links to directories;
  • -b delete, covering previously established links;
  • -f enforcement;
  • -n generally regarded as the symbolic link directory;
  • -v display detailed processing procedure;

So, summed up:

  • Create a soft link (symbolic link) use:ln -s source target
  • Create a hard link (physical link) use:ln source target

such as:

--------------------------------------------------------------------------------------------------------
leon@Ubuntu:~/temp$ ll													# 首先创建一个测试目录和测试文件
total 16
drwxrwxr-x  3 leon leon 4096 Aug 29 14:21 ./
drwxr-xr-x 23 leon leon 4096 Aug 29 14:20 ../
drwxrwxr-x  2 leon leon 4096 Aug 29 14:21 testdir/
-rw-rw-r--  1 leon leon   40 Aug 29 14:21 testfile
--------------------------------------------------------------------------------------------------------
leon@Ubuntu:~/temp$ ln testdir hard_link								# 不能对目录创建硬链接
ln: testdir: hard link not allowed for directory
--------------------------------------------------------------------------------------------------------
leon@Ubuntu:~/temp$ ln -s testdir soft_link_for_dir						# 可以对目录创建软链接
leon@Ubuntu:~/temp$ ll
total 16
drwxrwxr-x  3 leon leon 4096 Aug 29 14:26 ./
drwxr-xr-x 23 leon leon 4096 Aug 29 14:20 ../
lrwxrwxrwx  1 leon leon    7 Aug 29 14:26 soft_link_for_dir -> testdir/
drwxrwxr-x  2 leon leon 4096 Aug 29 14:21 testdir/
-rw-rw-r--  1 leon leon   40 Aug 29 14:21 testfile
--------------------------------------------------------------------------------------------------------
leon@Ubuntu:~/temp$ ln testfile hard_link_for_file						# 可以对文件创建硬链接
leon@Ubuntu:~/temp$ ll
total 20
drwxrwxr-x  3 leon leon 4096 Aug 29 14:27 ./
drwxr-xr-x 23 leon leon 4096 Aug 29 14:20 ../
-rw-rw-r--  2 leon leon   40 Aug 29 14:21 hard_link_for_file
drwxrwxr-x  2 leon leon 4096 Aug 29 14:21 testdir/
-rw-rw-r--  2 leon leon   40 Aug 29 14:21 testfile
--------------------------------------------------------------------------------------------------------
leon@Ubuntu:~/temp$ ln -s testfile soft_link_for_file					# 可以对文件创建软链接
leon@Ubuntu:~/temp$ ll
total 20
drwxrwxr-x  3 leon leon 4096 Aug 29 14:28 ./
drwxr-xr-x 23 leon leon 4096 Aug 29 14:20 ../
lrwxrwxrwx  1 leon leon    8 Aug 29 14:28 soft_link_for_file -> testfile
drwxrwxr-x  2 leon leon 4096 Aug 29 14:21 testdir/
-rw-rw-r--  2 leon leon   40 Aug 29 14:21 testfile
--------------------------------------------------------------------------------------------------------

Third, pay attention

1, create a soft link to the directory before, do not target directory is set up in advance, direct ln command will be established automatically. If, after you ahead of the establishment of a target directory, use the ln command to establish the target directory will be nested, which is to be careful!

leon@Ubuntu:~/temp$ ll testdir/												# 首先查看下源目录下的文件情况
total 8
drwxrwxr-x 2 leon leon 4096 Aug 29 14:29 ./
drwxrwxr-x 3 leon leon 4096 Aug 29 14:28 ../
-rw-rw-r-- 1 leon leon    0 Aug 29 14:29 test

# 下面的语句是直接创建软链接,可以看出软链接的结果是正确的
leon@Ubuntu:~/temp$ ln -s testdir soft_link_for_dir							# 创建软链接1
leon@Ubuntu:~/temp$ ll soft_link_for_dir									# 查看软链接1的详情
lrwxrwxrwx 1 leon leon 7 Aug 29 14:26 soft_link_for_dir -> testdir/

# 下面的语句是先创建好目标目录,然后再创建软链接。可以看出软链接的结果是不正确的
leon@Ubuntu:~/temp$ mkdir soft_link_for_dir2								# 先创建一个目录
leon@Ubuntu:~/temp$ ln -s testdir soft_link_for_dir2						# 然后再创建软链接2
leon@Ubuntu:~/temp$ ll soft_link_for_dir2									# 查看软链接2的详情
total 8
drwxrwxr-x 2 leon leon 4096 Aug 29 14:34 ./
drwxrwxr-x 4 leon leon 4096 Aug 29 14:34 ../
lrwxrwxrwx 1 leon leon    7 Aug 29 14:34 testdir -> testdir

2, when you create a soft link to the directory, do not carry the '/' character, or create a soft link points out the source directory will also carry a '/' symbol. While on the surface it appears to be a normal visit, but can not guarantee that in certain circumstances the problem occurs. It also should be careful!

leon@Ubuntu:~/temp$ ln -s testdir/ soft_link_for_dir1						# 不建议这么操作
leon@Ubuntu:~/temp$ ln -s testdir soft_link_for_dir2						# 这才是正确的
leon@Ubuntu:~/temp$ ll
total 16
drwxrwxr-x  3 leon leon 4096 Aug 29 15:41 ./
drwxr-xr-x 23 leon leon 4096 Aug 29 14:20 ../
lrwxrwxrwx  1 leon leon    8 Aug 29 15:40 soft_link_for_dir1 -> testdir//
lrwxrwxrwx  1 leon leon    7 Aug 29 15:41 soft_link_for_dir2 -> testdir/
drwxrwxr-x  2 leon leon 4096 Aug 29 14:29 testdir/
-rw-rw-r--  1 leon leon   40 Aug 29 14:21 testfile

3. When you remove soft links, can not carry the '/' character, or you remove that resource (ie source file) under the soft link target directory, rather than the soft link itself.

--------------------------------------------------------------------------------------------------------
leon@Ubuntu:~/temp$ rm -rf soft_link_for_dir1								# 这是正确的删除软链接的方式
leon@Ubuntu:~/temp$ ll														# 当前目录下的链接文件已经被删了
total 16
drwxrwxr-x  3 leon leon 4096 Aug 29 15:53 ./
drwxr-xr-x 23 leon leon 4096 Aug 29 14:20 ../
drwxrwxr-x  2 leon leon 4096 Aug 29 14:29 testdir/
-rw-rw-r--  1 leon leon   40 Aug 29 14:21 testfile
leon@Ubuntu:~/temp$ ls -al testdir/											# 并且,源目录下的原始文件还在
total 12
drwxrwxr-x 2 leon leon 4096 Aug 29 15:55 .
drwxrwxr-x 3 leon leon 4096 Aug 29 15:54 ..
-rw-rw-r-- 1 leon leon   18 Aug 29 15:53 new_file
--------------------------------------------------------------------------------------------------------
leon@Ubuntu:~/temp$ rm -rf soft_link_for_dir/								# 这是错误的删除软链接的方式
leon@Ubuntu:~/temp$ ll														# 当前目录下的链接文件没有被删掉
total 16
drwxrwxr-x  3 leon leon 4096 Aug 29 15:54 ./
drwxr-xr-x 23 leon leon 4096 Aug 29 14:20 ../
lrwxrwxrwx  1 leon leon    7 Aug 29 15:54 soft_link_for_dir -> testdir/
drwxrwxr-x  2 leon leon 4096 Aug 29 15:54 testdir/
-rw-rw-r--  1 leon leon   40 Aug 29 14:21 testfile
leon@Ubuntu:~/temp$ ls -al testdir/											# 而源目录下的原始文件却被删掉了
total 8
drwxrwxr-x 2 leon leon 4096 Aug 29 15:54 .
drwxrwxr-x 3 leon leon 4096 Aug 29 15:54 ..
--------------------------------------------------------------------------------------------------------

Guess you like

Origin blog.csdn.net/LEON1741/article/details/100136449