LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to get the absolute path of the running executable file and current work path ? (http://www.linuxquestions.org/questions/programming-9/how-to-get-the-absolute-path-of-the-running-executable-file-and-current-work-path-911068/)

915086731 10-31-2011 08:35 AM

How to get the absolute path of the running executable file and current work path ?
 
Hello,
I am developing with C , how to get the absolute path of the running executable file and current work path ?
I know getcwd can obtain the current work path, but how to get the path of current running file.
And how about shell script ?

meenakshi.khurana 10-31-2011 11:34 AM

Calling readlink on /proc/self/exe will give you the absolute path of the running binary

Code:


readlink("/proc/self/exe", buffer, BUFSIZ);
printf("%s\n", buffer);


Sergei Steshenko 10-31-2011 11:46 AM

Quote:

Originally Posted by 915086731 (Post 4512453)
Hello,
I am developing with C , how to get the absolute path of the running executable file and current work path ?
I know getcwd can obtain the current work path, but how to get the path of current running file.
And how about shell script ?

In http://256.com/sources/argv/argv.html look for "argv[0]". Yes, it can also be a symlink.

David the H. 11-01-2011 09:28 AM

Shell scripts work the same way. Just run the $0 parameter through the readlink utility.

Code:

readlink -f "$0"
For the current directory, of course, there's the pwd command and the $PWD shell variable.


All times are GMT -5. The time now is 11:10 PM.