Linux下学习ln命令笔记

来源:互联网 发布:淘宝退货入库 编辑:程序博客网 时间:2024/04/28 19:32

 

 

1.ln命令功能
  ln命令功能是为某一个文件在另外一个位置建立一个同步的链接;链接有两种:一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link),也可以叫软链接.

 


 
2.ln命令语法
ln [-s]  源文件 目标文件   
-s 是 symbolic的意思,即建立符号链接(Symbolic Link),不带-s参数下则建立硬链接(Hard Link).

 

 

3.软链接和硬链接
(1).软链接创建
[root@localhost ~]# su - Sunrier
[Sunrier@localhost ~]$ pwd
/home/Sunrier
[Sunrier@localhost ~]$ ls -l
总计 12
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test.c
[Sunrier@localhost ~]$ ln -s /home/Sunrier/test test.soft
[Sunrier@localhost ~]$ ls -l
总计 16
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]$ ln -s /home/Sunrier/test.c test1soft.c
[Sunrier@localhost ~]$ ls -l
总计 20
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1soft.c -> /home/Sunrier/test.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]$

说明:
当执行 ln -s /home/Sunrier/test test.soft 时,会在当前目录下创建软链接目录test.soft,test.soft目录指向/home/Sunrier/test真实目录地址;
当执行 ln -s /home/Sunrier/test.c test1soft.c 时,会在当前目录下创建软链接文件test1soft.c,test1soft.c文件指向/home/Sunrier/test.c真实文件地址
软链接有点像是 Windows 的『快捷方式』一样,->右边的内容表示真实目录(或文件)的地址

 

(2).硬链接创建
[Sunrier@localhost ~]$ pwd
/home/Sunrier
[Sunrier@localhost ~]$ ls -l
总计 20
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1soft.c -> /home/Sunrier/test.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]$ ln /home/Sunrier/test test.hard
ln: “/home/Sunrier/test”: 不允许将硬链接指向目录
[Sunrier@localhost ~]$ ln /home/Sunrier/test.c test1hard.c
[Sunrier@localhost ~]$ ls -l
总计 24
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1soft.c -> /home/Sunrier/test.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test1hard.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~] cp /home/Sunrier/test.c test1copy.c
[Sunrier@localhost ~]$ ls -l
总计 28
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1.c -> /home/Sunrier/test.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:57 test1copy.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test1hard.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]$ cp -p /home/Sunrier/test.c test1sametime.c
[Sunrier@localhost ~]$ ls -l
总计 32
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1.c -> /home/Sunrier/test.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:57 test1copy.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test1hard.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test1sametime.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]

说明:
硬链接不能链接目录,只能链接文件;硬链接与真实文件的大小和创建日期完全相同,有点类似于copy,
但是copy(拷贝)与真实文件的创建日期不同,copy可以通过加-p参数复制相同创建日期.

 

(3).软链接同步更新
软链接类似于 Windows 的快捷方式,访问的就是源文件,所有的跟源文件同步
它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间 


 
(4).硬链接同步更新
硬链接类似copy,同样也拥有同步更新功能
[Sunrier@localhost ~]$ pwd
/home/Sunrier
[Sunrier@localhost ~]$ ls -l
总计 32
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1.c -> /home/Sunrier/test.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:57 test1copy.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test1hard.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test1sametime.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]$ cat test1hard.c
[Sunrier@localhost ~]$ cat test.c
[Sunrier@localhost ~]$ cat test1.c
[Sunrier@localhost ~]$ echo "Hello World" >> test1hard.c
[Sunrier@localhost ~]$ cat test1hard.c
Hello World
[Sunrier@localhost ~]$ cat test.c
Hello World
[Sunrier@localhost ~]$ cat test1.c
Hello World
[Sunrier@localhost ~]$

为什么硬链接可以同步更新?(来自网络上一个不错的解释)
当系统要读取某一个档案的时候,就会先去读 inode table ,
然后再根据 inode 的信息到 block area 去将数据取出来利用!
而 hard link 就是直接再建立一个 inode 连结到档案放置的 block 区块.
也就是说进行 hard link 的时候,实际上你的档案内容不会改变,只是你在查询的时候,
利用原来的 inode 与后来的 inode 均可被指定到该档案放置的地点,因此两个档案的内容会是一样的!
所以读取任何一个 inode 的结果都是存取在同一个档案的内容就是了.
不过,这样一来就有个问题啦,因为 inode 是会连结到 block 区域去的,
而『目录』本身仅消耗 inode 而已,那么一来的话, hard link 不就不能 link 目录啦?对的!没错!
Hard link 有两个最大的限制:
不能跨 filesystem ,因为不同的 filesystem 有不同的 inode table 嘛;类似于windows内不能将C盘的硬链接放在D盘!
不能 link 目录!
而软链接可以放在任何文件系统.

 

(5).删除源文件后,软链接和硬链接的区别
[Sunrier@localhost ~]$ pwd
/home/Sunrier
[Sunrier@localhost ~]$ ls -l
总计 32
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1.c -> /home/Sunrier/test.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:57 test1copy.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test1hard.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test1sametime.c
-rw-rw-r-- 2 Sunrier Sunrier    0 08-10 14:18 test.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]$ rm test.c
[Sunrier@localhost ~]$ ls -l
总计 40
drwxrwxr-x 2 Sunrier Sunrier 4096 08-10 14:29 test
lrwxrwxrwx 1 Sunrier Sunrier   20 08-10 14:43 test1.c -> /home/Sunrier/test.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:57 test1copy.c
-rw-rw-r-- 1 Sunrier Sunrier   12 08-10 15:13 test1hard.c
-rw-rw-r-- 1 Sunrier Sunrier    0 08-10 14:18 test1sametime.c
lrwxrwxrwx 1 Sunrier Sunrier   18 08-10 14:27 test.soft -> /home/Sunrier/test
[Sunrier@localhost ~]$ cat test1.c
cat: test1.c: 没有那个文件或目录
[Sunrier@localhost ~]$ cat test1hard.c
Hello World
[Sunrier@localhost ~]$

说明:
软链接类似快捷方式,当源文件不存在时,它也无效;硬链接类似copy,仍然存在.

 

 

原创粉丝点击