Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file contents by inode in Bash?

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

like image 252
user619271 Avatar asked Apr 02 '17 07:04

user619271


People also ask

Does inode contain file content?

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.

How do I open an inode file in Linux?

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 .)

How do you list inodes?

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.


1 Answers

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.

like image 145
Derlin Avatar answered Oct 06 '22 01:10

Derlin