LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command to normalise paths (https://www.linuxquestions.org/questions/linux-newbie-8/command-to-normalise-paths-469397/)

Dtsazza 07-31-2006 10:38 AM

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!

raskin 07-31-2006 11:01 AM

FULLPATH=$( cd $ARG; /bin/pwd );

Dtsazza 07-31-2006 11:30 AM

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.

raskin 07-31-2006 11:39 AM

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.

Tinkster 07-31-2006 01:45 PM

Try readlink -f file



Cheers,
Tink


All times are GMT -5. The time now is 12:54 AM.