Kohl Kohlbrenner
Kohl Kohlbrenner

Reputation: 55

dynamic absolute path symlinks

Is there a way to create dynamic absolute paths for symlinks?

Every time I tried creating a symlink with a relative path, the link was resolving as broken. The only option I had was to create the symlink with an absolute path

    ex. ln -s $PATH/folder docroot/folder1 

While this worked, when I pushed the code to the remote server, the path was still looking within my home directory and causing a permissions denied error. So, I was wondering if there was a way to fake the absolute path?

File structure of project looks like this:

SiteName
  Docroot
     folder
     folder
     folder

Upvotes: 1

Views: 1726

Answers (1)

clarity123
clarity123

Reputation: 2046

Your original idea to use relative linking was perfectly appropriate, but most likely just wasn't implemented correctly. To correctly create relative symlinks:

  • Given directory structure:

    SiteName
      docroot
         folder
    
  • Your current working dir: SiteName

  • You want: docroot/folder1 -> docroot/folder

Try:

$ ln -s folder docroot/folder1

If you had the tree program, you could see the structure, as well as the symlink folder1 successfully pointing to docroot's folder:

$ tree
.
└── docroot
    ├── folder
    └── folder1 -> folder

3 directories, 0 files

Explanation

Your original attempt at relative links failed likely due to a common misunderstanding about what is required for the relative link:

Shadur's Unix & Linux answer mentions:

Symbolic links are relative to the location the link is in, not the location you were when you created the link. ...

So,

  • From folder1's perspective, relative path to folder is simply folder since they are siblings of the same directory
  • The command syntax is ln -s <relative path> <where to create new link>
  • When running the command from SiteName, the new link would be created at docroot/folder1

So the final command is ln -s folder docroot/folder1 to correctly create at docroot/folder1 to point to folder within the same directory.

So it will now work as long as you don't change their relative locations.

Upvotes: 3

Related Questions