LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2011, 08:35 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
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 ?
 
Old 10-31-2011, 11:34 AM   #2
meenakshi.khurana
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Fedora,Redhat
Posts: 19

Rep: Reputation: 5
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);
 
1 members found this post helpful.
Old 10-31-2011, 11:46 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by 915086731 View Post
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.
 
Old 11-01-2011, 09:28 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change Absolute path to Relative path sahil.jammu Linux - Newbie 2 05-31-2011 04:57 AM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
HTML + href absolute path does not work blizunt7 Programming 3 11-21-2005 04:59 PM
how to get absolute path of current executing C/C++ prog? Thinking Programming 2 10-10-2005 10:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration