我知道这个命令:
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。