UNIX Shell: Find Out Real Path Of File ( Directory )

Q. Some of my programs and scripts needs a real path and not a a symbolic link. How to I determine a real path of any directory under Linux / UNIX operating systems? How do I remove references to /./, /../ and extra '/' character in path?

A. To get physical path use realpath command. The realpath command uses the realpath() function to resolve all symbolic links, extra / characters and references to /./ and /../ in path. This is useful for shell scripting and security related applications.

realpath examples

Resolve symbolic link:
$ realpath /home
Sample output:

/usr/home

Remove characters:
$ realpath /etc//apache/.
$ realpath ./foo
$ realpath /../some/where///./../path/

realpath includes with many distro and UNIX operating system such as FreeBSD. To install realpath under Debian / Ubuntu Linux, enter:
$ sudo apt-get install realpath

readlink command

Please note that mostly the same functionality is provided by the -f option of the readlink command:
$ readlink -f /home

Further readings:

  • man pages readlink, realpath
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 4 comments… read them below or add one }

1 yoander 10.23.08 at 9:08 pm

I cant’t find realpath in CentOS. Coud you address me?

2 vivek 10.23.08 at 9:51 pm

Use readlink -f /path/to/file/or/dir command under RHEL / CentOS.

3 yoander 10.23.08 at 10:07 pm

Thanks!!!

4 andy 10.24.08 at 6:19 am

why you dont use the command
pwd -P
comes out of the box…

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , ,

Previous post: Freebsd makeinfo: not found Error and Solution

Next post: FreeBSD: Install PHP xcache Opcode Cacher