欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Linux系統(tǒng)上創(chuàng)建軟連接和硬連接的方法

 更新時間:2024年08月23日 09:12:33   作者:一口Linux  
這篇文章主要介紹了在Linux系統(tǒng)上創(chuàng)建軟連接和硬連接的方法,通過執(zhí)行 man ln 命令,可以看到這是在文件之間建立鏈接,而沒有提及是軟鏈接或硬鏈接,文中通過代碼和圖文介紹的非常詳細,需要的朋友可以參考下

在本指南中,我將介紹什么是鏈接以及如何更好地使用它們以及所需的概念。

通過執(zhí)行 man ln 命令,可以看到這是在文件之間建立鏈接,而沒有提及是軟鏈接或硬鏈接。

shashi@linuxtechi ~}$ man ln

類似地,命令 man link 描述為調用鏈接功能創(chuàng)建文件。

Soft Link

軟鏈接顧名思義就是創(chuàng)建到新文件的新鏈接。在這種情況下,新文件的節(jié)點號將指向舊文件。

Hard Link

在這種情況下,舊文件和新文件都將指向相同的索引節(jié)點號。

Symbolic Link

在某些 Unix/Linux 系統(tǒng)中,符號和軟鏈接都被視為相同的。但是實際的差異是,新的文件和舊文件將指向一個新的 Inode 號碼,這將完全取決于實施。

注意: 在許多情況下,符號和軟鏈接術語可以互換使用,但是人們必須知道什么時候用什么。

Creating Hard Link

(1) man ln 命令輸出如下

shashi@linuxtechi ~}$ man ln
ln  - make links between files

(2) ln 命令無參數(shù),輸出如下

shashi@linuxtechi ~}$ln
ln: missing file operand
Try 'ln --help' for more information.

(3) 在兩個文件之間創(chuàng)建硬鏈接,首先檢查文件是否存在,否則將創(chuàng)建文件,然后鏈接它們。

shashi@linuxtechi ~}$ ls -la
total 24
drwx------  4 root root 4096 Feb  6 15:23 .
drwxr-xr-x 23 root root 4096 Jan 25 16:39 ..
-rw-r--r--  1 root root 3122 Jan 25 16:41 .bashrc
drwx------  2 root root 4096 Feb  6 15:23 .cache
-rw-r--r--  1 root root    0 Jan 25 16:41 .hushlogin
-rw-r--r--  1 root root  148 Aug 17  2015 .profile
drwxr-xr-x  2 root root 4096 Jan 25 16:41 .ssh

(4) 使用 touch 命令創(chuàng)建文件

shashi@linuxtechi ~}$ touch 123.txt
shashi@linuxtechi ~}$ ls -l
total 0
-rw-r--r-- 1 root root 0 Feb  6 15:51 123.txt

(5) 使用 cat 命令將內容輸入到文件中,然后按 ctrl+c 保存并退出。

shashi@linuxtechi ~}$ cat > 123.txt
Welcome to this World!
^C

(6) 創(chuàng)建 123.txt 和 321.txt 文件之間的硬鏈接

shashi@linuxtechi ~}$ ln 123.txt 321.txt
shashi@linuxtechi ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 123.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt

(7) 檢查文件的索引節(jié)點號

shashi@linuxtechi ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 123.txt
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
$ cat 321.txt
Welcome to this World!

(8) 再創(chuàng)建一個名為 456.txt 的文件,并使用 ln 命令將其鏈接到 321.txt

shashi@linuxtechi ~}$  ls -li
total 12
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 123.txt
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 321.txt
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 456.txt
$ cat 456.txt
Welcome to this World!
shashi@linuxtechi ~}$   ls -l
total 12
-rw-r--r-- 3 root root 23 Feb  6 15:52 123.txt
-rw-r--r-- 3 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 3 root root 23 Feb  6 15:52 456.txt

(9) 當源文件或這些文件中的任何一個被刪除時,它不會影響其他文件。

shashi@linuxtechi ~}$ rm 123.txt
shashi@linuxtechi ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
shashi@linuxtechi ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
shashi@linuxtechi ~}$ cat 456.txt
Welcome to this World!

(10) 不允許跨目錄創(chuàng)建硬鏈接。

shashi@linuxtechi ~}$ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln abc def
ln: abc: hard link not allowed for directory

(11) 對一個文件內容的任何更改都將影響并相應地更改其他文件的內容。

shashi@linuxtechi ~}$ vi 321.txt
Welcome to this World!
You are welcomed to this new world
:wq
shashi@linuxtechi ~}$ ls -l
total 12
-rw-r--r-- 2 root root   59 Feb  6 16:24 321.txt
-rw-r--r-- 2 root root   59 Feb  6 16:24 456.txt
drwxr-xr-x 2 root root 4096 Feb  6 16:18 abc
shashi@linuxtechi ~}$ cat 456.txt
Welcome to this World!
You are welcomed to this new world

Creating Soft Link:

(1) 使用 touch 命令創(chuàng)建文件 src.txt,使用 cat 命令輸入內容,然后按 ctrl+c 保存并退出。

shashi@linuxtechi ~}$ touch src.txt
shashi@linuxtechi ~}$ cat > src.txt
Hello World
^C
shashi@linuxtechi ~}$ ls -l
total 4
-rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt

(2) 將目標文件創(chuàng)建為 dst.txt,并使用 ln -s 命令創(chuàng)建符號鏈接 (也稱為軟鏈接)。查看 dst.txt 文件的內容,可以看到與 src.txt 相同的內容。

shashi@linuxtechi ~}$ ln -s src.txt dst.txt
shashi@linuxtechi ~}$  ls -l
total 4
lrwxrwxrwx 1 root root  7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt
shashi@linuxtechi ~}$  cat dst.txt
Hello World

(3) 在符號鏈接的情況下,源文件和目標文件的 inode 號不同。在權限中出現(xiàn)字母 l,表明這些是鏈接。dst.txt ->src.txt 將是現(xiàn)在建立的新鏈接。

shashi@linuxtechi ~}$  ls -li
total 4
794584 lrwxrwxrwx 1 root root  7 Feb  6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt

(4) 允許對目錄進行符號創(chuàng)建

shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln -s abc def
$ ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb  6 16:34 abc
lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt

(5) 源和目標的 Inode 編號不同

shashi@linuxtechi ~}$  ls -li
total 8
794585 drwxr-xr-x 2 root root 4096 Feb  6 16:34 abc
794586 lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
794584 lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt

(6) 如前所述,可以為目錄創(chuàng)建符號鏈接。一旦創(chuàng)建了這些帶有符號鏈接的目錄,就可以在這些目錄中創(chuàng)建文件。當在源目錄中創(chuàng)建文件時,同樣的情況也會反映在目標目錄中。

shashi@linuxtechi ~}$ $ cd abc
shashi@linuxtechi ~}$  touch 123.txt
shashi@linuxtechi ~}$  vi 123.txt
Hello
:wq!
shashi@linuxtechi ~}$  touch 456.txt
shashi@linuxtechi ~}$  cd ..
shashi@linuxtechi ~}$  ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb  6 16:36 abc
lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt
shashi@linuxtechi ~}$ cd def
shashi@linuxtechi ~}$ ls -l
total 4
-rw-r--r-- 1 root root 6 Feb  6 16:37 123.txt
-rw-r--r-- 1 root root 0 Feb  6 16:36 456.txt
shashi@linuxtechi ~}$ cat 123.txt
Hello

Removing Soft Link / Symbolic Links

使用 rm 和 unlink 命令刪除軟鏈接或符號鏈接

# rm <soft-link-filename>
# unlink <soft-link-filename>

刪除軟鏈接目錄

# rm <soft-link-directory>
# unlink <soft-link-directory>

以上就是在Linux系統(tǒng)上創(chuàng)建軟連接和硬連接的方法的詳細內容,更多關于Linux創(chuàng)建軟連接和硬連接的資料請關注腳本之家其它相關文章!

相關文章

  • Linux中搭建coturn服務器的過程

    Linux中搭建coturn服務器的過程

    這篇文章主要介紹了Linux中搭建coturn服務器,首先下載coturn源碼,進入到coturn路徑下執(zhí)行相應命令,本文給大家介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • Linux bzip2 命令的使用

    Linux bzip2 命令的使用

    這篇文章主要介紹了Linux bzip2 命令的使用,幫助大家更好的理解和使用Linux系統(tǒng),感興趣的朋友可以了解下
    2020-08-08
  • Linux中rsync命令使用方式

    Linux中rsync命令使用方式

    rsync是一款高效的文件同步工具,支持增量同步、遠程同步、文件壓縮、權限保留等功能,它可以用于本地或遠程文件夾的同步,支持斷點續(xù)傳和排除規(guī)則,在本地模式下,可以通過cp命令替代,實現(xiàn)數(shù)據(jù)的增量備份,在遠程模式下
    2025-01-01
  • 分享9個實戰(zhàn)及面試常用Linux Shell腳本編寫

    分享9個實戰(zhàn)及面試常用Linux Shell腳本編寫

    這篇文章主要介紹了9個實戰(zhàn)及面試常用Shell腳本編寫,非常不錯,具有一定的收藏價值,需要的朋友可以參考下
    2018-10-10
  • IO復用之select poll epoll的總結(推薦)

    IO復用之select poll epoll的總結(推薦)

    下面小編就為大家?guī)硪黄狪O復用之select poll epoll的總結(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Linux安裝yum時出現(xiàn)apt-get?install?E:?無法定位軟件包問題解決

    Linux安裝yum時出現(xiàn)apt-get?install?E:?無法定位軟件包問題解決

    這篇文章主要介紹了Linux安裝yum時出現(xiàn)apt-get?install?E:?無法定位軟件包問題解決的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-12-12
  • linux NFS安裝配置及常見問題、/etc/exports配置文件、showmount命令

    linux NFS安裝配置及常見問題、/etc/exports配置文件、showmount命令

    這篇文章主要介紹了linux NFS安裝配置及常見問題,介紹的也比較詳細特分享下,方便需要的朋友
    2014-07-07
  • 判斷CC攻擊 netstat命令詳解

    判斷CC攻擊 netstat命令詳解

    判斷CC攻擊 netstat命令詳解,快速找出有問題的ip。
    2010-12-12
  • Linux中的進程狀態(tài)和優(yōu)先級

    Linux中的進程狀態(tài)和優(yōu)先級

    這篇文章主要介紹了Linux中的進程狀態(tài)和優(yōu)先級方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • linux壓縮解壓文件夾命令zip unzip和tar詳解

    linux壓縮解壓文件夾命令zip unzip和tar詳解

    本文介紹了如何使用zip和unzip命令處理.zip文件,以及如何使用tar命令處理.tar、.tar.gz、.tar.bz2和.tar.xz文件,包括壓縮、解壓、查看壓縮包內容、打包與壓縮、解壓到指定目錄、排除文件、遞歸壓縮和壓縮算法對比等內容
    2025-02-02

最新評論