bash 如何在bash中通过inode获取文件内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43165762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file contents by inode in bash?
提问by user619271
How can I retrieve file contents in bash by knowing only inode of the file?
如何通过只知道文件的 inode 来检索 bash 中的文件内容?
回答by Cyrus
Get inode with ext2/ext3/ext4 filesystem:
使用 ext2/ext3/ext4 文件系统获取 inode:
inode=262552
device=/dev/sda1
sudo debugfs -R "ncheck $inode" $device 2>/dev/null
Output (example):
输出(示例):
Inode Pathname 262552 /var/log/syslog
To show content of file:
显示文件内容:
sudo debugfs -R "ncheck $inode" $device 2>/dev/null | awk '/[1-9]/ {print }' | xargs cat
回答by Derlin
As per this unixexchange answer:
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 访问文件,因为这会破坏通过权限的访问控制。例如,如果您没有遍历目录的权限,那么无论该文件的权限如何,您都无法访问该目录中的任何文件。如果您可以通过 inode 访问文件,那将绕过目录权限。
There is some ways to get the path or name of a file through an inode number though, for example using find
. Once you get one path to the file, you can use any of the regular tools.
不过,有一些方法可以通过 inode 编号获取文件的路径或名称,例如使用find
. 获得文件的一个路径后,您可以使用任何常规工具。
find
has an inum
argument to look up files by inodes. Here is an example:
find
有一个inum
通过 inode 查找文件的参数。下面是一个例子:
find -inum 1704744 -exec cat {} \;
This will print the content of the file with inode 1704744
, assuming it is located in the current directory or one of its children.
这将使用 inode 打印文件的内容1704744
,假设它位于当前目录或其子目录之一。
Note: ls
also has a -i
option to get the inode associated with files.
注意:ls
还有一个-i
选项可以获取与文件关联的 inode。
回答by Inian
If you have access to find
with find (GNU findutils)
and notthe POSIX find
, you can use its -inum
option for it,
如果您有访问find
与find (GNU findutils)
和没有的POSIX find
,你可以用它 -inum
选择了它,
-inum n
File has inode number n. It is normally easier to use the
-samefile test instead
Just use it along with the -exec
option and open the file using cat
,
只需将它与-exec
选项一起使用并使用打开文件cat
,
find . -inum 1346087 -exec cat {} \;
Assume inode
value for my file sample.txt
is as
假设inode
我的文件的价值sample.txt
是
ls -id sample.txt
1346087 sample.txt
回答by jm666
The portable but slow way is using the find -inum ...
, for getting the filename for the given inode and in the next step use the found filename.
可移植但速度较慢的方法是使用find -inum ...
, 获取给定 inode 的文件名,并在下一步中使用找到的文件名。
If you need just the content
of the file (by the inode number), exists some file-system/OS specific solutions.
如果您只需要content
文件(通过 inode 编号),则存在一些特定于文件系统/操作系统的解决方案。
For the macOS it is simple as:
对于 macOS,它很简单:
inode=48747714
eval "$(stat -s "/Volumes/volume_name")"
cat /.vol/$st_dev/$inode
E.g. In the OS X's HFS filesystem exists the pseudo directory /.vol
, and you can use for accessing file contents by their inode number as:
例如,在 OS X 的 HFS 文件系统中存在伪目录/.vol
,您可以使用它们的 inode 编号访问文件内容,如下所示:
/.vol/device_id/inode_number
the device_id
is is obtainable from the stat
for the given volume_name
in the /Volumes
, (check ls /Volumes
).
的device_id
是从所得到的stat
用于给定volume_name
的/Volumes
,(检查ls /Volumes
)。