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:
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 05:17 PM..
it is a bit more complicated than that.
to be more specific, when given such a root folder that has foldera and folderb under it.
root---foldera
|-folderb
there is a file called afile in foldera.
right now, i am in folderb and I am trying to open afile by calling a function. the parameter of this function can be either "../foldera/afile" or "/root/foldera/afile". Now, in this function, I want to find out the absolute path of "afile" if the parameter is not absolute yet.
any help is appreciated
thanks
Last edited by filedeliver; 06-05-2007 at 04:57 PM..
You do realize there can be more than one hard link for a given file, plus there may be multiple symlinks as well. When you call fopen ("/path/to/foo", "r"), foo can easily be a link. So you may not have the real path to start with.
So going "up" the file tree is not going to give you a perfect answer all the time.
I'm not giving you a hard time, it's just that working out whre a file is in all the filesystems on one box is a daunting task. find / -name <> -inode <> is about as good as it gets, even if it is really slow.
In your example, if you're postive foldera or folderb or folderc are under /root then your task is:
get st_dev and st_ino for the file you do have
with popen(), "find /root -name afile -inode st_ino", then stat all of the files returned.
check the st_dev values, when you have a match you have the file.
If you are in, ahem, /directoryb (sorry, but I hate calling directories "folders"), you can call getcwd() to obtain "/directoryb". Since "../directorya/filea" does not start with a /, it must be a relative path. So prepend your current directory to it to get "/directoryb/../directorya/filea" which is an absolute path to the file in question. You could parse this to remove "/.." and the word preceding "/.." if you really need to simplify the path. But no matter what you do, it is possible for files to have multiple absolute paths that do not involve ".." due to symbolic links and loopback mounts.
Hello,
I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Hi,
Need to insert /usr/local/bin before scp in all the lines for the below using VI
#cat scpfile.txt
scp test.txt user@host1:
scp test.txt user@host2:
scp test.txt user@host3:
scp test.txt user@host4:
Regards,
Siva (2 Replies)
I don't understand the question below..any can help me? thanks
Display the absolute path of the executable used when a
'grep' command is entered on the command line:
---------- Post updated at 08:30 PM ---------- Previous update was at 08:29 PM ----------
absolute (2 Replies)
Hello Everyone,
I want to convert
Relative Path - /home/stevin/data/APP_SERVICE/../datafile.txt
to
Absolute Path - /home/stevin/data/datafile.txt
Is there a built-in tool in Unix to do this or any good ideas as to how can I implement this.
-Steve (5 Replies)
Hello,
I have a doubt:-
---------------------
Current script:-
################################################################################################
prefix=user@my-server:
find . -depth -type d -name .git -printf '%h\0' | while read -d "" path ; do (
cd "$path" || exit $?... (4 Replies)
I have a script in which i want to print absolute path of the same script irrespective of path from where i run script.
I am using
test.sh:
echo "pwd : `pwd`"
echo "script name: $0"
echo "dirname: `dirname $0`"
when i run script from /my/test/dir/struct as ../test.sh the output i... (10 Replies)
I'm having problems accessing the Knoppix software on my current computer and the replacement CD I ordered hasn't arrived yet.
I have a guess at what the answer would be for this question but I am not sure as I cannot test it with the software.
I have to create a directory called class, and... (1 Reply)
Hi,
I have a file with about 60 lines of path:
app-defaults/boxXYZ.......
I want to change this to /my/path/goes/here/app-defaults/boxXYZ, but of course vi doesn't like the regualr :s/old/new/ command.
Is there any other quick way to do this?
Thanks ;) (2 Replies)
In the funtion C function link(char *existing, char *new);
existing has to be an absolute path.
But what happens if i want to make a ling to a file in the users home directory (assume file.txt exists)
i cant put in a "~/file.txt" or "./file.txt"
How can i turn the above into the entire path... (1 Reply)