b97902HW 板


LINE

簡介 link, stat, chdir, opendir 我們在上一篇得到了不少檔案系統的基本概念了!不過在我介紹 link, stat, chdir, opendir 等 API 之前,我先介紹一下一些相關的指令。 相關指令 我第一個想要介紹的指令是 ln 與 unlink。這二個指令分別是用來建立 與刪除一個 Link。他們的使用方式如下: ln [-s] 目標路徑 檔案連結 unlink 檔案連結路徑 對與 ln 指令,我們可選擇我們要建立的連結是 Symbolic Link 還是 Hard Link。例如: $ echo content > test.txt $ ls -al 總計 12 drwxr-xr-x 2 logan logan 4096 2010-04-11 03:43 . drwxr-xr-x 5 logan logan 4096 2010-04-11 03:42 .. -rw-r--r-- 1 logan logan 8 2010-04-11 03:43 test.txt # 我們可以看到,只有 test.txt 這一個 Hard Link 指向那個 inode, # 而指向這個目錄的 inode 的 Hard Link 有二個。另外我們可以注 # 意到:test.txt 的檔案大小是 8bytes。 $ ln test.txt test2.txt # 建立一個新的 Hard Link $ ls -al 總計 16 drwxr-xr-x 2 logan logan 4096 2010-04-11 03:47 . drwxr-xr-x 5 logan logan 4096 2010-04-11 03:42 .. -rw-r--r-- 2 logan logan 8 2010-04-11 03:43 test2.txt -rw-r--r-- 2 logan logan 8 2010-04-11 03:43 test.txt # 我們可以注意到 test.txt 與 test2.txt 指向的 inode 其 st_nlink # 的值都是 2。而且二個檔案的大小都是 8bytes。 $ unlink test.txt # 我們可以用 unlink 去除 test.txt 這一個 Hard Link。我們會發 # 現為一個檔案建立一個新的 Hard Link 再使用 unlink,其效果類 # 似 rename。 $ ls -al 總計 12 drwxr-xr-x 2 logan logan 4096 2010-04-11 03:57 . drwxr-xr-x 5 logan logan 4096 2010-04-11 03:42 .. -rw-r--r-- 1 logan logan 8 2010-04-11 03:50 test2.txt $ cat test2.txt content $ ln -s test2.txt test3.txt # 嘗試建立 Symbolic Link $ ls -al 總計 12 drwxr-xr-x 2 logan logan 4096 2010-04-11 04:00 . drwxr-xr-x 5 logan logan 4096 2010-04-11 03:42 .. -rw-r--r-- 1 logan logan 8 2010-04-11 03:50 test2.txt lrwxrwxrwx 1 logan logan 9 2010-04-11 04:00 test3.txt -> test2.txt # 我們可以注意到:Symbolic Link 的顯示方式與 Hard Link 的顯 # 示方式略有不同。而且二者的 st_nlink 似乎都只有 1。另外, # Symbolic Link 的檔案大小也不同!似乎剛好就是 "test2.txt" # 這一個字串的長度。我們再看一個例子: $ ln -s /home/logan/桌面/tutorial/test2.txt test4.txt $ ls -al 總計 12 drwxr-xr-x 2 logan logan 4096 2010-04-11 04:05 . drwxr-xr-x 5 logan logan 4096 2010-04-11 03:42 .. -rw-r--r-- 1 logan logan 8 2010-04-11 03:50 test2.txt lrwxrwxrwx 1 logan logan 9 2010-04-11 04:00 test3.txt -> test2.txt lrwxrwxrwx 1 logan logan 37 2010-04-11 04:05 test4.txt -> /home/logan/桌面 /tutorial/test2.txt 如果一個檔案只剩一個 Hard Link,這個檔案還被 unlink 會怎樣? $ unlink test2.txt $ ls -al 總計 8 drwxr-xr-x 2 logan logan 4096 2010-04-11 04:08 . drwxr-xr-x 5 logan logan 4096 2010-04-11 03:42 .. lrwxrwxrwx 1 logan logan 9 2010-04-11 04:00 test3.txt -> test2.txt lrwxrwxrwx 1 logan logan 37 2010-04-11 04:05 test4.txt -> /home/logan/桌面 /tutorial/test2.txt # 我們可以注意到:test2.txt 好像就消失了!他的效果和 remove # 很像!事實上,只要一個檔案其 st_nlink 變成 0 其空間會被回收。 # 我們再看一個比較大的檔案: $ dd if=/dev/zero of=test.dat bs=4096 count=25600 $ df | grep "/home" /dev/sda7 39373712 13972728 23400896 38% /home $ unlink test.dat $ df | grep "/home" /dev/sda7 39373712 13870324 23503300 38% /home # 相差接近 102400kb! 接下來我們來試試其怪的 link: $ ln -s test5.txt test5.txt $ cat test5.txt cat: test5.txt: Too many levels of symbolic links $ touch test6_16 $ for i in {0..15}; do j=`echo $i+1|bc`; ln -s test6_$j test6_$i; done $ ls -al lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_0 -> test6_1 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_1 -> test6_2 lrwxrwxrwx 1 logan logan 8 2010-04-11 04:30 test6_10 -> test6_11 lrwxrwxrwx 1 logan logan 8 2010-04-11 04:30 test6_11 -> test6_12 lrwxrwxrwx 1 logan logan 8 2010-04-11 04:30 test6_12 -> test6_13 lrwxrwxrwx 1 logan logan 8 2010-04-11 04:30 test6_13 -> test6_14 lrwxrwxrwx 1 logan logan 8 2010-04-11 04:30 test6_14 -> test6_15 lrwxrwxrwx 1 logan logan 8 2010-04-11 04:30 test6_15 -> test6_16 -rw-r--r-- 1 logan logan 0 2010-04-11 04:30 test6_16 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_2 -> test6_3 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_3 -> test6_4 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_4 -> test6_5 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_5 -> test6_6 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_6 -> test6_7 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_7 -> test6_8 lrwxrwxrwx 1 logan logan 7 2010-04-11 04:30 test6_8 -> test6_9 lrwxrwxrwx 1 logan logan 8 2010-04-11 04:30 test6_9 -> test6_10 $ cat test6_8 # ok $ cat test6_7 # ELOOP 接下來我要談的是最後一個指令:stat。我們可以用 stat 指令來看 一個檔案的 inode 的相關資訊。使用方法如下: stat 檔案路徑 例如: $ stat test5.txt File: `test5.txt' -> `test5.txt' Size: 9 Blocks: 0 IO Block: 4096 symbolic link Device: 807h/2055d Inode: 925583 Links: 1 Access: (0777/lrwxrwxrwx) Uid: ( 1000/ logan) Gid: ( 1000/ logan) Access: 2010-04-11 04:12:21.961974851 +0800 Modify: 2010-04-11 04:12:20.741974396 +0800 Change: 2010-04-11 04:12:20.741974396 +0800 # 使用 stat 指令,我們可以看到一個檔案的 st_ino,st_nlink, # 檔案的種類,UID,Access Time 與 Modify Time。 $ stat test6_16 File: `test6_16' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 807h/2055d Inode: 925592 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ logan) Gid: ( 1000/ logan) Access: 2010-04-11 04:31:54.541996399 +0800 Modify: 2010-04-11 04:30:02.580974470 +0800 Change: 2010-04-11 04:30:02.580974470 +0800 # 這是一個 regular file 的範例 $ stat . File: `.' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 807h/2055d Inode: 925582 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 1000/ logan) Gid: ( 1000/ logan) Access: 2010-04-11 04:30:13.885998161 +0800 Modify: 2010-04-11 04:30:12.704972352 +0800 Change: 2010-04-11 04:30:12.704972352 +0800 # 這是一個 directory 的範例 $ ln -s . rec $ stat rec File: `rec' -> `.' Size: 1 Blocks: 0 IO Block: 4096 symbolic link Device: 807h/2055d Inode: 925609 Links: 1 Access: (0777/lrwxrwxrwx) Uid: ( 1000/ logan) Gid: ( 1000/ logan) Access: 2010-04-11 04:38:55.788996368 +0800 Modify: 2010-04-11 04:38:55.788996368 +0800 Change: 2010-04-11 04:38:55.788996368 +0800 # 這是一個 symbolic link 的範例,我直接把 rec 這個 symbolic # link 指向 " . " $ ln test6_16 test6_16-2 $ stat test6_16-2 File: `test6_16-2' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 807h/2055d Inode: 925592 Links: 2 Access: (0644/-rw-r--r--) Uid: ( 1000/ logan) Gid: ( 1000/ logan) Access: 2010-04-11 04:31:54.541996399 +0800 Modify: 2010-04-11 04:30:02.580974470 +0800 Change: 2010-04-11 04:40:21.604996580 +0800 # 這個範例是想要展示 stat 指令會怎麼處理一個 hard link。 # 我們可以看到 stat 指令是直接看那個檔案的 inode 資訊, # 所以既然 Hard Link 的 inode 一樣,顯示的結果也會一樣。 怎麼好像寫了半天還是文不對題? ... 未完.待續 -- LoganChien ----- from PTT2 個板 logan ----- --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.97.11 ※ 編輯: LoganChien 來自: 61.224.97.11 (04/11 04:51)
1F:推 Bingojkt:教學文全消推4@w< 04/19 18:16







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP