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

by Vivek Gite on October 3, 2008 · 5 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 5 comments… read them below or add one }

1 yoander October 23, 2008

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

Reply

2 vivek October 23, 2008

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

Reply

3 yoander October 23, 2008

Thanks!!!

Reply

4 andy October 24, 2008

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

Reply

5 olexiy July 24, 2009

pwd -P
works for folders only
if you have file as link to different file pwd -P will not help

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 4 + 13 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: