Bash如何通过inode获取文件内容?
How to get file contents by inode in Bash?
如何只知道文件的 inode 来检索 Bash 中的文件内容?
如果您可以使用 find (GNU findutils)
访问 find
而 而不是 POSIX find
,您可以使用它的 -inum
选项为此,
-inum n
File has inode number n. It is normally easier to use the
-samefile test instead
只需将它与 -exec
选项一起使用,然后使用 cat
、
打开文件
find . -inum 1346087 -exec cat {} \;
假设我的文件 sample.txt
的 inode
值为
ls -id sample.txt
1346087 sample.txt
You cannot access files by inodes, because that would break access control via permissions. For example, if you don't have the permission to traverse a directory, then you can't access any of the files in that directory no matter what the permissions on the file are. If you could access a file by inode, that would bypass directory permissions.
虽然有一些方法可以通过 inode 编号获取文件的路径或名称,例如使用 find
。获得文件的一个路径后,您可以使用任何常规工具。
find
有一个 inum
参数来通过 inode 查找文件。这是一个例子:
find -inum 1704744 -exec cat {} \;
这将打印 inode 1704744
文件的内容,假设它位于当前目录或其子目录之一。
注意:ls
还有一个-i
选项来获取与文件关联的索引节点。
使用 ext2/ext3/ext4 文件系统获取索引节点:
inode=262552
device=/dev/sda1
sudo debugfs -R "ncheck $inode" $device 2>/dev/null
输出(示例):
Inode Pathname
262552 /var/log/syslog
显示文件内容:
sudo debugfs -R "ncheck $inode" $device 2>/dev/null | awk '/[1-9]/ {print }' | xargs cat
可移植但速度较慢的方法是使用 find -inum ...
获取给定 inode 的文件名,并在下一步中使用找到的文件名。
如果您只需要文件的 content
(通过 inode 编号),存在一些 file-system/OS 特定解决方案。
对于 macOS 它很简单:
inode=48747714
eval "$(stat -s "/Volumes/volume_name")"
cat /.vol/$st_dev/$inode
例如在 OS X 的 HFS 文件系统中存在伪目录 /.vol
,您可以通过 inode 编号访问文件内容:
/.vol/device_id/inode_number
对于 /Volumes
中给定的 volume_name
,device_id
可从 stat
获得(检查 ls /Volumes
)。
如何只知道文件的 inode 来检索 Bash 中的文件内容?
如果您可以使用 find (GNU findutils)
访问 find
而 而不是 POSIX find
,您可以使用它的 -inum
选项为此,
-inum n
File has inode number n. It is normally easier to use the
-samefile test instead
只需将它与 -exec
选项一起使用,然后使用 cat
、
find . -inum 1346087 -exec cat {} \;
假设我的文件 sample.txt
的 inode
值为
ls -id sample.txt
1346087 sample.txt
You cannot access files by inodes, because that would break access control via permissions. For example, if you don't have the permission to traverse a directory, then you can't access any of the files in that directory no matter what the permissions on the file are. If you could access a file by inode, that would bypass directory permissions.
虽然有一些方法可以通过 inode 编号获取文件的路径或名称,例如使用 find
。获得文件的一个路径后,您可以使用任何常规工具。
find
有一个 inum
参数来通过 inode 查找文件。这是一个例子:
find -inum 1704744 -exec cat {} \;
这将打印 inode 1704744
文件的内容,假设它位于当前目录或其子目录之一。
注意:ls
还有一个-i
选项来获取与文件关联的索引节点。
使用 ext2/ext3/ext4 文件系统获取索引节点:
inode=262552
device=/dev/sda1
sudo debugfs -R "ncheck $inode" $device 2>/dev/null
输出(示例):
Inode Pathname 262552 /var/log/syslog
显示文件内容:
sudo debugfs -R "ncheck $inode" $device 2>/dev/null | awk '/[1-9]/ {print }' | xargs cat
可移植但速度较慢的方法是使用 find -inum ...
获取给定 inode 的文件名,并在下一步中使用找到的文件名。
如果您只需要文件的 content
(通过 inode 编号),存在一些 file-system/OS 特定解决方案。
对于 macOS 它很简单:
inode=48747714
eval "$(stat -s "/Volumes/volume_name")"
cat /.vol/$st_dev/$inode
例如在 OS X 的 HFS 文件系统中存在伪目录 /.vol
,您可以通过 inode 编号访问文件内容:
/.vol/device_id/inode_number
对于 /Volumes
中给定的 volume_name
,device_id
可从 stat
获得(检查 ls /Volumes
)。