Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:34:50+00:00 2026-05-24T04:34:50+00:00

Suppose I create a symbolic link file2 to a file file1 two directories above

  • 0

Suppose I create a symbolic link “file2” to a file “file1” two directories above the current location (e.g., “ln -s ../../file1 file2”. But now suppose that “file1” is also a symbolic link to a file “file0″ two directories down from its location (say its relative path is dir1/dir2/file0”).

I’d prefer if the “file1” symbolic link contains a relative URL going to file0 “../../dir1/dir2/file0” rather than just “../../file1”. That latter only indirectly points to the file.

What’s a good way of doing this?

I could hack together something with readlink. But I’m still hoping there’s an “better” way that I’m not considering or have overlooked.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:34:50+00:00Added an answer on May 24, 2026 at 4:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I create a Windows Azure application that consists of multiple instances talking to
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
Suppose I am designing a class that can handle any database technology to create
Suppose i create a thread that ,in some point, calls a function foo(). If
Suppose I create a file for writing like this: std::ofstream my_file(filename, std::ios_base::out | std::ios_base::trunc);
This is a very contrived example, but let's suppose we create a variable _this
How can I change the input port in racket? That is, suppose I create
I am using the standard WinSock functions for socket communication, Now suppose I create
Suppose I create an experimental branch in TFS. Now, I am finished with the
I am suppose to Create a form that gives the user 3 different options

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.