-   Programming (
-   -   Getting file name from inode number (

kalamaraki 02-24-2007 12:20 PM

Getting file name from inode number
Hello again! I have a question. I am writing a program in C. If I have a filename, I can find its inode number by using struct stat. Is there a function that works the other way around? I mean, if I have the inode number, can I find the filename? Or, at least, ONE of the filenames? I know that there can be multiple filenames that have the same inode number, but they actually refer to the same file in the file system.

What I want to do is create hard links to inode numbers. For example, if there is a file file1.txt with inode number 123456, I want to create another file called file2.txt with the same inode number. I have a variable that holds the initial inode number. Searching the man pages, I found 'link' command:

link(const char *oldpath, const char *newpath)

I have the newpath, but I want to find the oldpath. I have the inode number. Any ideas?

Thanks in advance.

bigrigdriver 02-24-2007 01:10 PM


if I have the inode number, can I find the filename?
You can do it from a console with the ls -i command. Add the -R (recursive) to list subdirectories. ls -i lists inode numbers and filenames. ls -i | grep '<inode number>' will find the inode number, and show you the file name. So, I'd say it can be done.

Suggestion: look at the source code for ls to see how it's done.

kalamaraki 02-24-2007 01:31 PM

Thanks for the reply, but I want to do this in the source code. I mean, I don't want to do it in the terminal window, because it is something I want to use further in my code.

slzckboy 02-24-2007 02:57 PM

here ya go


slzckboy 02-24-2007 03:00 PM

oops,sorry ignore last post.I didn't read your question properly..

KenJennings 02-24-2007 09:17 PM

As far as I know the only direct association between filename and inode occurs inside the directory structure. So, if you need to actually implement this in C internally, then you would have to re-invent the wheel by duplicating what the find command does to recursively traverse directories and examine all the directory entries.

Alternatively, if you can use system() to call the find command try system("find -inum your_inum_here"); and capture the output to a temporary file, then read the contents of the temporary file.

Too0ld2c0de 11-16-2018 09:39 AM

C code to find filenames matching inode number
You can use nftw(3) or ftw(3) to recursively walk part of the file tree. These library functions will call a routine you provide which will get the stat(2) structure for each file encountered along with its filename. You can check the stat's st_ino field to see if it matches your target inode number and if so, grab the filename that was passed to it. Let me know if you'd like to see a code fragment that shows this.

NevemTeve 11-16-2018 10:35 AM

That would flink(2) -- sadly, there is no such thing.

BW-userx 11-16-2018 10:50 AM

going back to what @bigrigdriver said, look at the source code to see how it is done, ls is written in C as well.

search for inode, and whatever else you need.
for find to see how it does it.

astrogeek 11-16-2018 02:19 PM

@ Too0ld2c0de: You have replied to a thread which has been inactive for more than 11 years.

If you have a question or wish to share your knowledge it is always preferable to open your own thread. This will allow it to attract the attention of currently active members and allow others to see your questions and information in the context specific to your issues, rather than having to parse possibly outdated information in the older thread.

Welcome to LQ, and good luck!

mina86 11-20-2018 03:57 PM

Since this thread has been dug up, I guess there’s no harm in adding one more post explaining reasons for a lack of such a system call.

One cannot create a link to an inode (or open an inode) because that would circumvent filesystem permissions. To be able to open a file, user must have permissions to that file (which are stored in its inode so that’s fine) and all directories on path its path. As long as user had a write permission on some directory, user would be able to read all files its permissions allow them to do even if normally that would be blocked by directory permissions.

All times are GMT -5. The time now is 09:32 PM.