The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



Thread: absolute path
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 06-05-2007
jim mcnamara jim mcnamara is offline
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,373
You mean that you're given a filename like "foo.txt" and you want to find it in the filesystem to see that it's in a given path, example: /somepath/to/myfiles/foo.txt?

stat the file, get the inode number of the file, call popen with
ls -i /somepath/to/myfiles/foo.txt
and check the inode you get back. It has to match the inode number you started with.

The other case is nasty - when you have no idea where the file is located.
The reason is that the one unique identifier for a file in a given filesystem is the inode.
It can be duplicated in all of the other filesystems mounted on the machine.
So you could have several foo.txt files with the same inode number.

You can try using ftw() or nftw(), or call find from a popen() call.
It is not efficient to use either of these from the root directory /, plus it is possible to find more than one matching filename/inode.

The find syntax is:
Code:
find / -type f  -name foo.txt -inode <inode number>
Edit: note that st_dev plus st_inode give a unique identifier for a file.
You have to call ftw(), as there is no way to identify an st_dev value for find to use.

Last edited by jim mcnamara; 06-05-2007 at 01:17 PM..
Reply With Quote