問題1:我創建了一個硬鏈接文件,但是我不知道放在那裏了,請問怎麼辦?
思路:
- 查找文件可以利用find命令。
- 硬鏈接的特性,不會跨文件系統,所以源文件在哪個文件系統中,就在那裏找
- 硬鏈接文件和源文件具有相同的inode號,可以利用find的-inum參數和-samefile參數來查找。
- -inum參數表示按照inode號去查找,首先要查看文件的inode號
- -samefile參數表示查找和某個文件相同的inode的文件,不用查看文件的inode號
[root@]# stat test.txt //獲取文件的inode號碼
File:`test.txt'
Size:55 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 148677 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-04-17 15:32:13.861206307 +0800
Modify: 2016-04-17 14:20:13.246213893 +0800
Change: 2016-04-17 16:04:17.023206128 +0800
[root@]# find / -inum 1486772 >/dev/null //根據Inode號碼查找,把錯誤提示丟棄
/usr/local/111test
/data/oldboy/linktest.txt
問題2:我對文件n創建了一個軟連接文件,但是我不知道放在哪裏了,也不知道叫什麼名字,請問怎麼辦?
思路:
- 軟鏈接可以跨文件系統,並且inode、屬性都和源文件的不同沒有規律可以尋
- 當我們使用ls –l命令查看鏈接文件的時候,它都會以:filename -> link filename方式顯示。
- 我們尋找當前系統內所有的鏈接文件(需要在根下面),然後過濾我們要找的源文件名n,就能查看有哪些文件指向了源文件。
- 注意這裏的示例爲了節省速度直接在當前文件夾內進行了測試
[root@]# find -type l -exec ls -l {} \;
lrwxrwxrwx 1 root root 1 Apr 17 16:21 ./111test.txt-> 1
lrwxrwxrwx 1 root root 10 Apr 17 16:16./linktest.txt -> test.txt
//找到文件後,長格式顯示,然後過濾源文件名
[root@]# find -type l -exec ls -l {}\;| grep 'linktest.txt'
lrwxrwxrwx 1 root root 10 Apr 17 16:16 ./linktest.txt -> test.txt