I need in a bash script to get details about a file when I know the inode.The system is Linux.
3 Answers
If you're dealing exclusively with an ext2/3 filesystem, you can use debugfs to do your inode to file look-up, which can be considerably faster than using find for large filesystems with many files.
debugfs -R "ncheck $inode" /dev/device 2> /dev/null | tail -1 | awk '{print $2}'
Find is still really your best bet though, there is nothing else I know of that is filesystem agnostic.
-
1good idea, but I think this still needs to brute force it, and you don't have the luxury of narrowing down the path. My system took about the same time to locate an inode with debugfs as it did with find.– JimBNov 21, 2008 at 23:43
-
Thumbed up for the info. However, on my ext4 root partition with 181,558 inodes (which isn't really "large"),
sudo find / -mount -inum $i
is consistently faster thansudo debugfs -R "ncheck $i" /dev/sda1
(~0.28s vs ~0.52s). Can anyone with a large filesystem test this command?– netvopeOct 15, 2013 at 20:38 -
Did you invalidate your buffer cache before testing?
sync; sync; echo 3 > /proc/sys/vm/drop_caches
Oct 16, 2013 at 12:13
Something like so:
find $SEARCHPATH -maxdepth $N -inum $INUM -exec ls -l {} \;
Since the filename links to the inode, ans not vice-versa, you need to do this in a brute force manner. The -maxdepth is to narrow it down if you have some idea of where it should be. You can also ad -xdev if your searching a tree containing multiple filesystems.
You can use find
with a combination of -inum
and -xdev
. This gives you the file's names (it can have more than one name), and from them you can find whatever information you want.