absolute path


 
Thread Tools Search this Thread
Top Forums Programming absolute path
# 1  
Old 06-05-2007
absolute path

How to find out the absolute path of a file in C/C++?
Thanks
# 2  
Old 06-05-2007
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 05:17 PM..
# 3  
Old 06-05-2007
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..
# 4  
Old 06-05-2007
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.
# 5  
Old 06-05-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

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)
Discussion started by: Sekhar419
4 Replies

2. UNIX for Dummies Questions & Answers

Insert absolute path in VI

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)
Discussion started by: ksgnathan
2 Replies

3. UNIX for Dummies Questions & Answers

Display the absolute path...

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)
Discussion started by: wk9031
2 Replies

4. UNIX for Dummies Questions & Answers

How to convert relative path to absolute path?

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)
Discussion started by: qwarentine
5 Replies

5. Shell Programming and Scripting

How to change Absolute path to Relative path

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)
Discussion started by: sahil_jammu
4 Replies

6. Shell Programming and Scripting

absolute path for a script ran with relative path

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)
Discussion started by: rss67
10 Replies

7. UNIX for Dummies Questions & Answers

Help with absolute path and relative path

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)
Discussion started by: mzero
1 Replies

8. UNIX for Dummies Questions & Answers

absolute path

is cd ~ considered an absolute path? (2 Replies)
Discussion started by: Kirichiko
2 Replies

9. UNIX for Dummies Questions & Answers

vi - replacing a relative path with absolute path in a file

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)
Discussion started by: Yinzer955i
2 Replies

10. Programming

HOW to make absolute path???? HELP

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)
Discussion started by: youngvet
1 Replies
Login or Register to Ask a Question