LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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: 120
Blog Entries: 5

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: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 01:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration