[aklug] Re: relative to absolute path

From: Michael Fowler <michael@shoebox.net>
Date: Wed Aug 27 2008 - 14:35:54 AKDT

On Tue, Aug 26, 2008 at 09:50:23PM -0800, bryanm@acsalaska.net wrote:
> I really, really should know this, but I can't come up with the
> trick. What shell trick can I use to print a file's full pathname?
> For example, if I specify dir/file or ~/file or just file, how can
> I turn that into the full path?

If you're using ~/file in the shell it should already be expanded into a
full path automatically. dir/file can be made absolute in the shell
with "$PWD/dir/file" or "`pwd`/dir/file".

If the paths you're using exist you can use Perl's abs_path to resolve
them:

    perl -MCwd=abs_path -wle 'print abs_path($_) for @ARGV' dir/file

This has the benefit of resolving .. and ., as well as symlinks:

> perl -MCwd=abs_path -wle 'print "$_ = ", abs_path($_) for @ARGV' \
> foo foo/bar ../../lib ../././mail /var/mail

    foo = /home/michael/foo
    foo/bar =
    ../../lib = /lib
    ../././mail = /home/mail
    /var/mail = /var/spool/mail

/var/mail is a symlink to /var/spool/mail on this system, and I have no
/home/michael/foo.

As a simple stand-alone program to print out the absolute path for each
argument:

    #!/usr/bin/perl
    use Cwd qw(abs_path);
    print abs_path($_), "\n" for @ARGV;

--
Michael Fowler
www.shoebox.net
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed Aug 27 14:36:12 2008

This archive was generated by hypermail 2.1.8 : Wed Aug 27 2008 - 14:36:13 AKDT