The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
List files with full path mr_bold Shell Programming and Scripting 2 01-23-2008 10:00 PM
to find the file with full path surjyap Shell Programming and Scripting 5 01-18-2008 11:26 AM
Listing files with full path r_sethu UNIX for Dummies Questions & Answers 4 08-02-2007 06:35 PM
Finding relative path of a file chiru_h Shell Programming and Scripting 4 04-16-2007 05:20 PM
vi - replacing a relative path with absolute path in a file Yinzer955i UNIX for Dummies Questions & Answers 2 09-07-2006 08:47 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2007
Registered User
 

Join Date: Mar 2007
Posts: 2
getting full path from relative path

given a relative path, how do i convert it into a full one. i.e. if i am in

/home/polypus

and i am given foo/bar then to get a full path i can just concatinate it with pwd, but what if i am given "../mama"

how do i programmatically convert:

/home/polypus and ../mama into /home/mama

in a way that will always work no matter what relative path is passed?

my first thought is to just cd to the directory and set a variable

cd $rev_path
full_path=`pwd`
cd $original_dir

it seems there must be a unix command which does this more elegantly?

thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-15-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,502
This is why relative paths have problems sometimes. There isn't a good way to handle absolutely any relative path, because all relative paths have an assumption:

the current working directory is "x".

You are assuming /home/polypus. Unix tools are designed to do one thing very well. It's hard to make a bulletproof tool because of the cwd assumption.

Because:
Obviously it may not be true.

So, there is no really elegant way to deal with it. You're onto one way to deal with it.
For programming, you should consider avoiding relative paths unless you want the code to work if and only if it runs from a certain directory.
Reply With Quote
  #3 (permalink)  
Old 03-15-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,356
Suppose that I don't have room in /usr for /usr/local but I do have some room in /export. So I make a directory called /export/usr.local and it has a subdirectory called bin. And I do a "ln -s /export/usr.local /usr/local". Now I do a "cd /usr/local" then I do a "cd bin". At this point my view is that I am in /usr/local/bin and a shell with a built-in pwd command may return that path. But /usr/bin/pwd will return /export/usr.local/bin. With more symbolic links in a path, there can be more aliases like this. The value returned by /usr/bin/pwd is the physical path. With good permissions on each directory leading to the current directory, /usr/bin/pwd can return the physical path. A built-in pwd in the same shell that navigated to the current directory may be able to return the particular logical path used to arrive at the current directory. Finding all logical paths to a particular directory would be rather daunting.

Writing a program like /usr/bin/pwd is not very easy in unix. You can stat the . directory to get the inode of the current directory. Then you can open .. and stat each file in the parent directory until you find the same inode. You walk up the chain one .. at a time repeating this process until . and .. are the same inode which means you have reached /. (Actually it is harder than that because you might traverse a mount point.) This is why putting an NFS mounted filesystem in / is very unwise... every /usr/bin/pwd must contact the NFS server to perform the stat.
Reply With Quote
  #4 (permalink)  
Old 03-17-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
not a 'silver bullet', but.....
Code:
#!/bin/ksh

thisFILE=$(whence ${0})
echo "here is my absolute pathname: [${thisFILE}]"
Reply With Quote
  #5 (permalink)  
Old 03-25-2007
Registered User
 

Join Date: Mar 2007
Posts: 2
thanks

thanks guys for explanations and help
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0