LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-27-2006, 02:28 PM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Smile Find file/dir path


Hello everyone. Can anybody tell me how can I get the absolute path of a file/directory? Thank you.
 
Old 08-27-2006, 02:33 PM   #2
nilleso
Member
 
Registered: Nov 2004
Location: ON, CANADA
Distribution: ubuntu, RHAS, and other unmentionables
Posts: 372

Rep: Reputation: 31
pwd will give you your present working directory.
...no sure what your asking for... do you know the filename?

if so you can use find / -name yourfilename to locate a specific file on disk.
Or you can use (slocate yourfilename)

If this doesn't answer your needs please repost specifics.
cheers
 
Old 08-27-2006, 02:36 PM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
You are right I was ambigous. I know the relative path of the file.
Do you think I should use pwd to find out the absolute path to the curent directory and then join this path with the relative given path?
I don't know..

Last edited by nc3b; 08-27-2006 at 02:38 PM.
 
Old 08-27-2006, 03:49 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Well, there's a problem with using "pwd" to get the current directory. If you are in a symlinked directory, "pwd" won't give you the absolute path.
For instance, I have a symlinked directory, called '/kdm-sessions', which points to '/usr/share/apps/kdm/sessions', and if I go "cd /kdm-sessions; pwd", I get "/kdm-sessions".
 
Old 08-27-2006, 04:00 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try that one:
Code:
realPath()
{
  if [ -d $1 ]
  then
    (cd $1; pwd)
  else
    if [ -f $1 ]
    then
      echo $(cd $(dirname $1); pwd)/$(basename $1)
    else
      echo $1 not found
    fi
  fi
}
 
Old 08-28-2006, 04:49 AM   #6
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
This is great! Thank you
 
  


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
howto add a dir to the path? vegetassj2 Linux - Newbie 11 08-16-2006 02:02 PM
how do i get the proper path to a file through find command napster Linux - General 4 11-07-2005 04:35 AM
Changing home dir path? NonSumPisces Linux - Newbie 4 08-22-2004 04:16 PM
adding a dir to PATH for root Villain Linux - Newbie 3 06-20-2004 03:49 PM
PWD question : Current dir not full path facets Programming 2 06-09-2004 04:58 AM

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

All times are GMT -5. The time now is 04:17 AM.

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