How can I retrieve file contents in Bash by knowing only the inode of the file?
Basically, the inode contains all information about a file outside of the actual name of the file and the actual data content of the file. The full inode structure can be found in the header file /usr/include/jfs/ino.
Whether you can open a file depends not only on its own access permission bits, but on the permission bits of every containing directory. (For instance, in your example, if test. txt were mode 644 but the containing directory test were mode 700, then only root and the owner of test could open test. txt .)
To get the number of inodes of files in a directory, for example, the root directory, open a terminal window and run the following ls command, where the -l option means long listing format, -a means all files and -i mean to print the index number of each file.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With