Linux下学习ln命令笔记


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 的快捷方式,访问的就是源文件,所有的跟源文件同步
它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间

  • 1
  • 2
  • 下一页

相关内容