About Linux FAQ

Browse More FAQs:

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

Posted by Vivek Gite [Last updated: October 3, 2008]

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

E-mail    Print    Can't find an answer to your question? Contact us

Related Other Helpful FAQs:

Discussion on This FAQ

  1. yoander Says:

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

  2. vivek Says:

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

  3. yoander Says:

    Thanks!!!

  4. andy Says:

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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

Tags: , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Powered by Open source software.