ln command
Function description
The ln command is used to create links between files or directories.
There are two kinds of links under Linux, one is hard link, the other is symbolic link (symbolic link is often called soft link), the default is to use the LN command to create a hard link.
Number is called the inode number (Inode Index). Multiple files pointing to the same inode are allowed in the Linux system, which is called hard links. The purpose of a hard link is to allow a file to have multiple valid path names so that users can establish hard links to important files to prevent accidental deletions. Because there is more than one link to the inode of the file, deleting only one link does not affect the inode itself and other links, only if the last link is deleted, the file's data block and the directory's link will be released, that is, the file will be really deleted at this time.
Also called a soft link, a soft link is similar to a shortcut to Windows, so a soft link is a symbolic connection that points to a real file or directory location.
Use the following
Hard Link ln File link Soft link ln-s Item link Here the item can be a file or it can be a directory |
Command parameters
Options |
Meaning |
-F |
If there is a file with the same link name in the target location, the file will be deleted |
-S |
Make a soft link |
-D |
Allow system administrators to hard-link their own directories |
-B |
Back up files that will be overwritten or deleted at link time |
Example
Hard-Link a file without parameters
[[email protected] shell]# ln test.sh htest.sh   #对源文件test. SH Create a hard link [Email protected] shell]# ls-li #可以看到二个文件inode号相同 Total Dosage 8 68033834 -rw-r--r--. 2 root root 4 November 16:47 htest.sh 68033834 -rw-r--r--. 2 root root 4 November 16:47 test.sh [[Email protected] shell]# ln htest.sh/tmp/ #链接到另一个目录, the target can not write the file name [Email protected] shell]# ls-li/tmp/ #相同名称的硬链接, the inode number is the same Total Dosage 32 68033834 -rw-r--r--. 4 root root 4 November 16:47 htest.sh 101282617-RW-------. 1 root root 26843 November 15:58 yum_save_tx.2016.r6hacv.yumtx [Email protected] shell]#
|
Use the-b parameter to back up the link first
[[email protected] shell]# ln-b htest.sh test.sh #-b hard link before backup [[email protected] shell]# ls-li #以 ~ The end is the backup file Total Dosage 12 68033834-rw-r--r--. 4 root root 4 November 16:47 htest.sh 68033834-rw-r--r--. 4 root root 4 November 16:47 test.sh 68033834-rw-r--r--. 4 root root 4 November 16:47 test.sh~ |
- S a soft link that links the/etcinittab file symbol to the/opt
[email protected] ~]# ln-s/etc/inittab/opt [Email protected] ~]# ls-li/opt/inittab #下面是软链接的显示 with different inode numbers 26186 lrwxrwxrwx. 1 root root 12 November 18:02 /opt/inittab-/etc/inittab [Email protected] ~]# Ls-li/etc/inittab 34140601 -rw-r--r--. 1 root root 511 March 6 2015/etc/inittab |
This article is from the "Zhao Dongwei blog" blog, make sure to keep this source http://zhaodongwei.blog.51cto.com/4233742/1871880
Linux commands: ln command