Operating System - HP-UX
1819678 Members
3573 Online
109605 Solutions
New Discussion юеВ

HPUX command to return full pathname of a file

 
SOLVED
Go to solution
Steve Bonds
Trusted Contributor

HPUX command to return full pathname of a file

I'm looking for the hpux equivalent of the linux "realpath" command. HPUX has a realpath() system call, but no command for it. Although I could just whip up a simple program that calls realpath(), I'm hoping for something more portable.

Here's an example of what realpath does:

$ cd /long/directory/name
$ touch testfile
$ realpath testfile
/long/directory/name/testfile

I've been through the man pages ("man -k pathname") and searched the forums a couple of different ways, but couldn't find an obvious answer. Any suggestions on where to look?
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: HPUX command to return full pathname of a file

Hi:

'dirname' is what you are seeking. Its counterpart is 'basename'.

The same thing can be achieved with shell builtin parameter substitution (faster) too.

Regards!

...JRF...
John Poff
Honored Contributor
Solution

Re: HPUX command to return full pathname of a file

Hi,

Have you tried 'whence'? I think whence just looks in the directories specified in your $PATH variable.

JP
John Palmer
Honored Contributor

Re: HPUX command to return full pathname of a file

There isn't a HP-UX equivalent. I reckon that writing a simple C program is your best bet.

For portability, you could write a shell script but filenames that include . .. and soft links wouldn't be easy to handle.

Regards,
John

Steve Bonds
Trusted Contributor

Re: HPUX command to return full pathname of a file

From James Ferguson:

>'dirname' is what you are seeking. Its counterpart is 'basename'.

Unfortunately, this doesn't quite do what I want. Since the file is in the current working directory, all it returns is ".".

> The same thing can be achieved with shell builtin parameter substitution (faster) too.

I don't quite follow you here. Could you give an example?

From John Poff:

> Have you tried 'whence'? I think whence just looks in the directories specified in your $PATH variable.

I thought this as well, but whence will search for nonexecutable files that are not in the PATH.

This does just what I need, thanks!

Too bad the whence command doesn't exist in linux. Oh, well, that's why we have if statements...

-- Steve
James R. Ferguson
Acclaimed Contributor

Re: HPUX command to return full pathname of a file

Hi (again) Steve:

Yes, I read your post's title and made the assumption I did. Sorry. Here's what I meant:

#!/usr/bin/sh
F=/var/adm/syslog/syslog.log
echo `dirname $F`
echo ${F%/*}
echo `basename $F`
echo ${F##*/}
exit 0

Regards!

...JRF...
john korterman
Honored Contributor

Re: HPUX command to return full pathname of a file

Hi,
try making a file called "realpath.sc" with this content:

function realpath
{
if [ $# = 1 ]
then
( cd $(dirname $1); echo "$PWD/$(basename $1)"; )
else
echo "Usage: realpath "
fi
}

source it, e.g.:
# . ./realpath.sc

Then try:
# realpath

regards,
John K.
it would be nice if you always got a second chance
Rodney Hills
Honored Contributor

Re: HPUX command to return full pathname of a file

You could do the following-
echo `pwd -H`/testfile

Since "cd" follows logical links, "pwd -H" will display the current path of the REAL directories.

HTH

-- Rod Hills

There be dragons...
Steve Bonds
Trusted Contributor

Re: HPUX command to return full pathname of a file

James: Thanks for the clarification.

John K: That looks like an excellent suggestion. I may have to try that. ;-)

-- Steve
John Palmer
Honored Contributor

Re: HPUX command to return full pathname of a file

The system call 'realpath' however returns a path that doesn't involve symbolic links. I still maintain that scripting this wouldn't be easy.

If you are not concerned with symlinks then as indicated, it's relatively simple.

Regards,
John
Steve Bonds
Trusted Contributor

Re: HPUX command to return full pathname of a file

Rodney:

That's a good suggestion, but it would break if the user put in an absolute filename. I.e. /path/to/file would be reinterpreted as (potentially) /path/to/path/to/file.

-- Steve