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 | Easy (rss) |
Root privileges | No |
Requirements | Bash/Ksh/Perl |
Time | N/A |
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:
/usr/home
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:
#!/usr/bin/perl use Cwd 'abs_path'; if (@ARGV != 2 ) { print "Usage: $0 --path file\n"; exit; } $realfilepath = abs_path("$ARGV[1]"); print "$realfilepath\n";
Sample outputs:
Recommended readings:
- man pages: readlink and realpath
- To see Cwd document type: perldoc Cwd
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 12 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
I cant’t find realpath in CentOS. Coud you address me?
Use readlink -f /path/to/file/or/dir command under RHEL / CentOS.
Thanks!!!
why you dont use the command
pwd -P
comes out of the box…
pwd -P
works for folders only
if you have file as link to different file pwd -P will not help
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?
or even a perl function!
Try:
Run it as:
thanks…
alias realpath=’perl -we ‘\”use Cwd; print Cwd::abs_path($_), “\n” foreach @ARGV;’\’
abs_path=`(cd .; pwd)`
Let me correct myself; the following is the right solution that’ll always work:
abs_path=`(cd \`dirname $0\`; pwd)`