歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下學習ln命令筆記

Linux下學習ln命令筆記

日期:2017/2/28 15:43:27   编辑:Linux教程

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 的快捷方式,訪問的就是源文件,所有的跟源文件同步
它只會在你選定的位置上生成一個文件的鏡像,不會占用磁盤空間

Copyright © Linux教程網 All Rights Reserved