How to get file contents by inode in Bash?

user619271 picture user619271 · Apr 2, 2017 · Viewed 11.3k times · Source

How can I retrieve file contents in Bash by knowing only the inode of the file?

Answer

Derlin picture Derlin · Apr 2, 2017

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.

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.

find has an inum argument to look up files by inodes. Here is an example:

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.

Note: ls also has a -i option to get the inode associated with files.