Is it possible to get the filename of a file descriptor (Linux) in C?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
readlinkon/proc/self/fd/NNNwhere NNN is the file descriptor. This will give you the name of the file as it was when it was opened — however, if the file was moved or deleted since then, it may no longer be accurate (although Linux can track renames in some cases). To verify,statthe filename given andfstatthe fd you have, and make surest_devandst_inoare the same.Of course, not all file descriptors refer to files, and for those you’ll see some odd text strings, such as
pipe:[1538488]. Since all of the real filenames will be absolute paths, you can determine which these are easily enough. Further, as others have noted, files can have multiple hardlinks pointing to them – this will only report the one it was opened with. If you want to find all names for a given file, you’ll just have to traverse the entire filesystem.