LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-31-2006, 10:38 AM   #1
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 31
Smile Command to normalise paths


Hi guys,

A nice easy one here (for someone who knows at least!). Very simple question - let's say I'm in directory /foo/bar/bin. What are my options to convert a pathname like ".." into /foo/bar? The simpler, and more importantly portable, the better - I'd like to avoid bash-isms if at all possible.

I know there's an easy way to do this, and I think I've even used it myself in the past and forgotten what it is... I tried various invocations of eval and passing arguments into pwd, but to no avail.

Thanks!
 
Old 07-31-2006, 11:01 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 68
FULLPATH=$( cd $ARG; /bin/pwd );
 
Old 07-31-2006, 11:30 AM   #3
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Original Poster
Rep: Reputation: 31
Mmm... that would work on the whole, but it feels a little clunky and awkward. Besides, it doesn't work with symlinks in the way I'd want it to:

Let's further assume that /foo/bar/share is a symlink to /usr/share. Then if we're in directory /foo/bar/bin, I want "../share" to be expanded into "/foo/bar/share". However, CDing into the argument and issuing pwd would return "/usr/share".

The command I had in mind, IIRC, essentially performed variable substitution on the supplied string, such that "~" at the start was transformed into the absolute path of your home directory, etc. I could write something similar myself as a short shell script, but distributing shell scripts isn't an option for me - I need something that'll run in /bin/sh on most *Nixes.
 
Old 07-31-2006, 11:39 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 68
Well, remove /bin/ in pwd: it was intentional because for my last case I needed real absolute path. Just pwd will tell you what shell thinks is absolute path.
 
Old 07-31-2006, 01:45 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Try readlink -f file



Cheers,
Tink
 
  


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
different paths iomari Linux - General 1 11-10-2004 06:57 AM
Where are my paths? colly Linux - General 1 06-17-2004 07:15 AM
paths 'command not found' christer Linux - Newbie 7 09-28-2003 01:27 PM
Automatically resolving WINDOWS paths to pre-configured Linux paths gazzy Linux - General 1 09-05-2003 10:15 PM
Attack Paths cottonmouth Linux - Security 6 12-27-2002 05:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:04 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration