UNIX Shell: Find Out Real Path Of File or Directory

See all Bash/Shell scripting related FAQ
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 like operating systems? How do I remove references to /./, /../ and extra ‘/’ character in path?

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.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash/Ksh/Perl
Est. reading time 1 minutes

Another option is readlink command to print value of a symbolic link or canonical file name.

Finally, you can use Perl or Python to print value of a symbolic link or canonical file name.

realpath examples

To resolve symbolic link, type:
$ realpath /home
Sample outputs:


To remove characters, run:
$ realpath /etc//apache/.
$ realpath ./foo
$ realpath /../some/where///./../path/

The realpath command included with most Linux distributions and UNIX like 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

Perl program example

Try the following Perl code for portability purpose under various Linux and Unix like oses:

use Cwd 'abs_path';
if (@ARGV != 2 ) {
  print "Usage: $0 --path file\n";
$realfilepath = abs_path("$ARGV[1]");
print "$realfilepath\n";

Sample outputs:

Fig.01: Perl program in action

Fig.01: Perl program in action

Recommended readings:
  • man pages: readlink and realpath
  • To see Cwd document type: perldoc Cwd

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

12 comments… add one
  • yoander Oct 23, 2008 @ 21:08

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

  • 🛡️ Vivek Gite (Author and Admin) nixCraft Oct 23, 2008 @ 21:51

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

  • yoander Oct 23, 2008 @ 22:07


  • andy Oct 24, 2008 @ 6:19

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

  • olexiy Jul 24, 2009 @ 23:59

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

  • Anthony Thyssen Mar 8, 2013 @ 5:34

    realpath and even reallink are NOT generally available on all linux machines (most unusual). It is certainally not available on solaris or other OS’s.

    And yet the C library provides a “realpath” function, it is just not generally available to shell scripts. And my shell scripts generally have to deal with multiple OS environments.

    So lets repeat the question…

    How can you get the real path of a file with all symbolic links resolved on MOST unix operating systems. Is some special switch to some command available? Is there a drop in shell function that can be used?

  • Anthony Thyssen Mar 8, 2013 @ 5:35

    or even a perl function!

    • 🛡️ Vivek Gite (Author and Admin) nixCraft Mar 8, 2013 @ 8:35


      use Cwd 'abs_path';
      if (@ARGV != 2 ) {
        print "Usage: $0 --path file\n";
      $realfilepath = abs_path("$ARGV[1]");
      print "$realfilepath\n";

      Run it as:

      ./myrealpath.pl --path /foo/bar
      ./myrealpath.pl --path ../foo/bar
      ./myrealpath.pl --path ~/bar
      ./myrealpath.pl --path /path/to/dir
      • Anthony Thyssen Mar 12, 2013 @ 1:52


      • Leif Jun 28, 2013 @ 15:22

        alias realpath=’perl -we ‘\”use Cwd; print Cwd::abs_path($_), “\n” foreach @ARGV;’\’

  • A Nov 6, 2013 @ 12:39

    abs_path=`(cd .; pwd)`

    • A Nov 7, 2013 @ 5:22

      Let me correct myself; the following is the right solution that’ll always work:

      abs_path=`(cd \`dirname $0\`; pwd)`

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.