ln
ln -s [src_file] [dest_file]
-s 创建软链接
软链接和硬链接的区别
- 两者的作用对象不同:
硬链接,只能应用于文件,而不能应用于目录,而且不能跨文件系统(即分区)。
软链接,可以应用于文件,而且可以应用于目录和可跨文件系统(即分区)。
- 两者的作用原理不同:
假如说A文件是B文件的硬链接文件,那么A和B的inode节点号相同,即一个inode节点对应两个不同的文件名,两个文件名指向同一个文件,A和B对文件系统来说是完全平等的,硬链接中一个inode号可以对应多个文件。如果删除其中任何一个文件,另外一个文件还能连接数据块,还是有效的文件,只是对应的inode节点号减少,其它并无影响。只有inode节点号减少到0,数据块才会被系统回收。
iPad:~# ln testfile.txt testfile.txt_hard_link
iPad:~# ls -ail
total 12
735789 drwx------ 5 root root 160 Feb 21 13:34 .
735591 drwxr-xr-x 20 root root 640 Feb 21 13:01 ..
736125 -rw------- 1 root root 195 Feb 21 13:34 .ash_history
736126 -rw-r--r-- 2 root root 51 Feb 21 13:33 testfile.txt
736126 -rw-r--r-- 2 root root 51 Feb 21 13:33 testfile.txt_hard_link
而在软链接中,假如说A文件是B文件的软连接文件,则A和B的inode节点号不同,每创建一个新的软链接,就会有一个新的节点号。而A的数据块中存放的只是B的路径名(可以根据这个找到B的目录项)。A和B之间是“主从”关系,如果B被删除了,A仍然存在(因为两个是不同的文件),但指向的是一个无效的链接。软链接类似于windows中的快捷方式。
iPad:~# ln -s testfile.txt testfile.txt_soft_link
iPad:~# ls -ail
total 16
735789 drwx------ 6 root root 192 Feb 21 13:40 .
735591 drwxr-xr-x 20 root root 640 Feb 21 13:01 ..
736125 -rw------- 1 root root 371 Feb 21 13:41 .ash_history
736126 -rw-r--r-- 2 root root 49 Feb 21 13:35 testfile.txt
736126 -rw-r--r-- 2 root root 49 Feb 21 13:35 testfile.txt_hard_link
736127 lrwxrwxrwx 1 root root 12 Feb 21 13:40 testfile.txt_soft_link -> testfile.txt
Soft links
Pointers to programs, files, or directories located elsewhere (just like Windows shortcuts)
If the original program, file, or directory is renamed, moved, or deleted, the soft link is broken.
If you type ls -F you can see which files are soft links because they end with @
To create a soft link called myfilelink.txt that points to a file called myfile.txt, use this: ln -s myfile.txt myfilelink.txt
Hard links
Pointers to programs and files, but NOT directories
If the original program or file is renamed, moved, or deleted, the hard link is NOT broken
Hard links cannot span disk drives, so you CANNOT have a hard link on /dev/hdb that refers to a program or file on /dev/hda
To create a hard link called myhardlink.txt that points to a file called myfile.txt, use this: ln myfile.txt myhardlink.txt