Reputation: 5002
How can I retrieve file contents in Bash by knowing only the inode of the file?
Upvotes: 22
Views: 19008
Reputation: 9891
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.
Upvotes: 14
Reputation: 88563
Get inode with ext2/ext3/ext4 filesystem:
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 $2}' | xargs cat
Upvotes: 14
Reputation: 63892
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.
If you need just the content
of the file (by the inode number), exists some file-system/OS specific solutions.
For the macOS it is simple as:
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:
/.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
).
Upvotes: 4
Reputation: 85550
If you have access to find
with find (GNU findutils)
and not the POSIX find
, you can use its -inum
option for it,
-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
,
find . -inum 1346087 -exec cat {} \;
Assume inode
value for my file sample.txt
is as
ls -id sample.txt
1346087 sample.txt
Upvotes: 8