Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

getting full path from relative path

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 03-14-2007
polypus polypus is offline
Registered User
Join Date: Mar 2007
Last Activity: 25 March 2007, 6:26 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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


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
cd $original_dir

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

Sponsored Links
Old Unix and Linux 03-15-2007
jim mcnamara jim mcnamara is offline Forum Staff  
Join Date: Feb 2004
Last Activity: 25 April 2017, 7:27 PM EDT
Location: NM
Posts: 11,022
Thanks: 515
Thanked 1,045 Times in 968 Posts
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.

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.
Sponsored Links
Old Unix and Linux 03-15-2007
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 469 Times in 270 Posts
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.
Old Unix and Linux 03-17-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
Join Date: Feb 2005
Last Activity: 26 April 2017, 4:21 AM EDT
Location: Foxborough, MA
Posts: 8,132
Thanks: 219
Thanked 762 Times in 713 Posts
not a 'silver bullet', but.....


thisFILE=$(whence ${0})
echo "here is my absolute pathname: [${thisFILE}]"

Sponsored Links
Old Unix and Linux 03-25-2007
polypus polypus is offline
Registered User
Join Date: Mar 2007
Last Activity: 25 March 2007, 6:26 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

thanks guys for explanations and help
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to change Absolute path to Relative path sahil_jammu Shell Programming and Scripting 4 05-31-2011 10:29 AM
How to extract strings from full path when full path is not fixed Shirisha Shell Programming and Scripting 0 10-07-2010 04:08 AM
absolute path for a script ran with relative path rss67 Shell Programming and Scripting 10 04-13-2010 07:48 AM
Help with absolute path and relative path mzero UNIX for Dummies Questions & Answers 1 03-02-2009 09:29 AM
vi - replacing a relative path with absolute path in a file Yinzer955i UNIX for Dummies Questions & Answers 2 09-07-2006 11:47 AM

All times are GMT -4. The time now is 05:46 AM.