tags:

views:

413

answers:

3

I need in a bash script to get details about a file when I know the inode.The system is Linux.

A: 

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.

CesarB
+1  A: 

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.

JimB
+3  A: 

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.

Steve Baker
good 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.
JimB