我知道這個指令:
find /path/to/mountpoint -inum <inode number>
但這是一個非常慢的搜索,我覺得必須有一種更快的方法來做到這一點。有人知道更快的方法嗎?
答案1
對於 ext4 檔案系統,您可以使用debugfs
下列範例:
$ sudo debugfs -R 'ncheck 393094' /dev/sda2 2>/dev/null
Inode Pathname
393094 /home/enzotib/examples.desktop
答案不是立即的,但似乎比 更快find
。
可以輕鬆解析輸出debugfs
以取得檔案名稱:
$ sudo debugfs -R 'ncheck 393094' /dev/sda2 | cut -f2 | tail -n2 > filenames
答案2
BTFS
inode-resolve [-v] <ino> <path>
(needs root privileges)
resolve paths to all files with given inode number ino in a given
subvolume at path, ie. all hardlinks
Options
-v
verbose mode, print count of returned paths and ioctl()
return value
例子:
sudo btrfs inspect-internal inode-resolve 15380 /home
答案3
基本問題是大多數檔案系統中都沒有按此方向工作的索引。如果您需要經常執行此類操作,最好的選擇是設定計劃任務來掃描檔案系統以獲取您需要的信息,建立一個包含您需要的資訊的資料庫(例如使用 sqlite3)並在其上建立索引用於快速定位檔案的索引節點號。
例子:
#!/bin/bash
# Generate an index file
#
SCAN_DIRECTORY=/
DB_DIRECTORY=~/my-sqlite-databases
if [ ! -d ${DB_DIRECTORY} ] ; then
mkdir ${DB_DIRECTORY}
fi
# Remove any old database - or use one created with a filename based on the date
rm ${DB_DIRECTORY}/files-index.db
(
# Output a command to create a table file_info in the database to hold the information we are interested in
echo 'create table file_info ( inode INTEGER, filepath, filename, numlinks INTEGER, size INTEGER);'
# Use find to scan the directory and locate all the objects - saving the inode, file path, file name, number of links and file size
# This could be reduced to just the inode, file path and file name ... if you are looking for files with multiple links the numlinks is useful (select * from file_info where numlinks > 1)
# Find output formats
#
# %i = inode
# %h = path to file (directory path)
# %f = filename (no directory path)
# %n = number of hard links
# %s = size
# Use find to generate the SQL commands to add the data to the database table.
find $SCAN_DIRECTORY -printf "insert into file_info (inode, filepath, filename, numlinks, size) values ( %i, '%h', '%f', %n, %s);\n"
# Finally create an index on the inode number so we can locate values quickly
echo 'create index inode_index on file_info(inode);'
# Pipe all the above commands into sqlite3 and have sqlite3 create and populate a database
) | sqlite3 ${DB_DIRECTORY}/files-index.db
# Once you have this in place, you can search the index for an inode number as follows
echo 'select * from file_info where inode = 1384238234;' | sqlite3 ${DB_DIRECTORY}/files-index.db
答案4
您可以查看 fsdb 命令,該命令在大多數 Unices 上都可以找到,我確信 Linux 上也可以使用該命令。這是一個強大的命令,允許您存取檔案的核心 inode 結構,所以要小心。語法也非常簡潔。
雖然 fsdb 實際上不會讓您發現 inode 的檔案名,但它做允許您在指定 inode 時直接存取它,本質上是將您「移植」到檔案本身(或至少是資料區塊指標),因此在這方面它比 find 更快;-)。
您的問題沒有指定您要如何處理該文件。您可能正在解碼 NFS 檔案句柄嗎?
SC。