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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:58:00  来源:igfitidea点击:

How to get file contents by inode in bash?

bashfilesystemsinode

提问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:

根据此 unixexchange 答案

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. 获得文件的一个路径后,您可以使用任何常规工具。

findhas an inumargument 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: lsalso has a -ioption to get the inode associated with files.

注意ls还有一个-i选项可以获取与文件关联的 inode。

回答by Inian

If you have access to findwith find (GNU findutils)and notthe POSIX find, you can use its -inumoption for it,

如果您有访问findfind (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 -execoption and open the file using cat,

只需将它与-exec选项一起使用并使用打开文件cat

find . -inum 1346087 -exec cat {} \;

Assume inodevalue for my file sample.txtis 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 contentof 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_idis is obtainable from the statfor the given volume_namein the /Volumes, (check ls /Volumes).

device_id是从所得到的stat用于给定volume_name/Volumes,(检查ls /Volumes)。