On Tue, Aug 9, 2011 at 1:36 PM, Bob Proulx <b...@proulx.com> wrote: > Jon Seymour wrote: >> readlink -f will fully resolve links in the path itself (rather than >> link at the end of the path), which was the behaviour I needed. > > Ah, yes, well, as you could tell that was just a partial solution > anyway. > >> It seems cd -P does most of what I need for directories and so >> handling things other than directories is a small tweak on that. > > You might try cd'ing there and then using pwd -P to get the canonical > directory name. I am thinking something like this: > > #!/bin/sh > p="$1" > dir=$(dirname "$p") > base=$(basename "$p") > physdir=$(cd "$dir"; pwd -P) > realpath=$(cd "$dir"; ls -l "$base" | awk '{print$NF}') > echo "$physdir/$realpath" | sed 's|//*|/|g' > exit 0 > > Again, another very quick and partial solution. But perhaps something > good enough just the same.
> realpath=$(cd "$dir"; ls -l "$base" | awk '{print$NF}') I always use sed for this purpose, so: $(cd "$dir"; ls -l "$base" | sed "s/.*->//") But, with pathological linking structures, this isn't quite enough - particularly if the target of the link itself contains paths, some of which may contain links :-) jon.