Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full path without resolving symlinks?

Tags:

bash

How can I get the full path to a symlink without resolving it to the path of the original file?

For example, if I have a directory set up like this:

~$ tree analysis_results/
analysis_results/
`-- Sample1
    `-- bar.txt -> /Users/me/foo.txt

If I try to get the full path to bar.txt, I end up with the path to foo.txt instead:

~$ readlink -f analysis_results/Sample1/bar.txt
/Users/me/foo.txt

But what I really want is this:

/Users/me/analysis_results/Sample1/bar.txt

Is there a simple way to accomplish this? It looks like I do not have realpath available, not sure if that program does this, or if there is some other command that works like this.

Things get more complicated when my pwd is something like:

/Users/me/analysis/peaks/workflow/code

But I want to read the full path to a symlink like:

/Users/me/analysis/alignment/results/Sample1/bar.txt
like image 612
user5359531 Avatar asked Jan 27 '17 19:01

user5359531


2 Answers

With GNU realpath:

$ touch foo
$ ln -s foo bar
$ realpath --no-symlinks bar
/path/to/bar
like image 56
bishop Avatar answered Sep 19 '22 19:09

bishop


echo "$(readlink -f analysis_results/Sample1)/bar.txt"

Resolve only the directory and then append the filename to the result.

like image 23
PSkocik Avatar answered Sep 22 '22 19:09

PSkocik