This will recursively test a file and retrieve a complete relative path, then remove unnecessary ". /" and "foo! /bin/bash linkfile="$1" while test -L "$linkfile" ; do linkfile="$(dirname ${linkfile})/$(readlink ${linkfile})" done perl -e '$x=shift; while ($x =~ s#//+#/#g) {} ; while ($x =~ s#/\.
/#/#g) {} ; while ($x =~ s#/(^\. |^\. ^/+?
|\. ^\. +?)/\.
\. /#/#g) {} ; while ($x =~ s#^(^\. |^\.
^/+? |\. ^\.
+? )/\. \.
/#/#g) {} ; $x =~ s#^\. /##; print "$x\n";' "$linkfile If you save that as "canonical. Bash", then you use it $ ln -s `canonical.
Bash foo` bar The regex won't reduce a few cases like "/..x/../" and "../foo/" (if foo/ is in your $PWD), but should be otherwise sturdy.
This will recursively test a file and retrieve a complete relative path, then remove unnecessary ". /" and "foo/../" #! /bin/bash linkfile="$1" while test -L "$linkfile" ; do linkfile="$(dirname ${linkfile})/$(readlink ${linkfile})" done perl -e '$x=shift; while ($x =~ s#//+#/#g) {} ; while ($x =~ s#/\.
/#/#g) {} ; while ($x =~ s#/(^\. |^\. ^/+?
|\. ^\. +?)/\.
\. /#/#g) {} ; while ($x =~ s#^(^\. |^\.
^/+? |\. ^\.
+? )/\. \.
/#/#g) {} ; $x =~ s#^\. /##; print "$x\n";' "$linkfile" If you save that as "canonical. Bash", then you use it $ ln -s `canonical.
Bash foo` bar The regex won't reduce a few cases like "/..x/../" and "../foo/" (if foo/ is in your $PWD), but should be otherwise sturdy.
This script seems to answer my question as asked. It does not however delivery the shortest relative path in all cases. It fails for this example: let there be "/home/user/dir1" which contains a directory "dir2" which contains "file.
Txt". Let "link1" also exist there and point to "dir2/file. Txt" and "link2" also exist and point to "../dir1/../dir1/link1".
The script returns "../dir1/dir2/file. Txt" instead of "dir2/file. Txt" as expected.
– Dr. Person Person II Aug 3 at 21:08 I am unsatisfied by having to resort to perl. A solution without it would be more elegant. – Dr. Person Person II Aug 3 at 21:14 As you saw in the other question you linked to, transforming a relative path into a canonical one is challenging.
I threw a quick regex to handle some of it and there are Perl modules that can handle it better, but I don't know of a standard shell command to accomplish it. – brightlancer Aug 7 at 3:25.
No, this is so specific that I think you'll have to use readlink and implement it yourself.
Now I remember! I saw a utility which accomplishes this some 15 years ago! See man symlinks(1)!
Symlinks -c. If this is not what you want, I suppose your needs are so specific you would have to code it yourself... (if I remember well, I also made some utility for handling symlinks on my disk...). PERL is a great language for that.
This is an interesting tool (was in repos for Ubuntu 10.04) but it doesn't do what I am trying to accomplish. I've found a couple bash scripts here at StackOverflow that half solve the problem but nothing that's completely satisfactory. I'm consider investing the time to write my own now.
Was hoping not to have to do that. I'll post if I write something that works that I'm proud of. – Dr. Person Person II Jul 23 at 14:01.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.