{"id":362,"date":"2021-06-20T10:21:34","date_gmt":"2021-06-20T09:21:34","guid":{"rendered":"https:\/\/amasan.co.uk\/vandragt\/?p=362"},"modified":"2021-06-20T10:28:26","modified_gmt":"2021-06-20T09:28:26","slug":"the-trick-to-relative-symlinks","status":"publish","type":"post","link":"https:\/\/2022.vandragt.com\/technology\/the-trick-to-relative-symlinks\/","title":{"rendered":"The trick to relative symlinks"},"content":{"rendered":"\n<p>I learned this week that relative symbolic links are relative to the link not the current working directory. What does this mean?<\/p>\n\n\n\n<p>To remind ourselves, the format of <code>ln<\/code> is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ln -s {{\/path\/to\/file_or_directory}} {{path\/to\/symlink}}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Say we have the following structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>config\/config.yaml\napp\/index.php<\/code><\/pre>\n\n\n\n<p>Say we want to create a symlink to <code>app\/config.yaml<\/code>. If we are familar with the <code>cp<\/code> command we would <strong>incorrectly<\/strong> do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"has-inline-color has-luminous-vivid-amber-color\">$ ln -s config\/config.yaml app\/config.yaml  #this is incorrect<\/span><\/code><\/pre>\n\n\n\n<p>Here, we&#8217;re asking <code>ln<\/code> to create a symlink in <code>app\/config.yaml<\/code> that points to <code>app\/config\/config.yaml<\/code> because the &#8216;source&#8217; parameter is relative to the link being created, not the current working directory. As that file does not exist the command fails!<\/p>\n\n\n\n<p><strong>Correct<\/strong> example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"has-inline-color has-light-green-cyan-color\">$ ln -s ..\/config\/config.yaml app\/config.yaml #this is correct\n$ cd app; ln -s ..\/config\/config.yaml config.yaml; cd - #this is clearer<\/span><\/code><\/pre>\n\n\n\n<p>The first correct example  can be difficult to comprehend unless we&#8217;re familiar with this concept: From the perspective of the <code>app\/config.yaml<\/code> file, we need to first go to the parent directory then into the config directory. <\/p>\n\n\n\n<p>This is clarified by the third example:  by changing into the destination directory, we align the current working directory with the path to the source. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Notes<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>If we create absolute symbolic links by providing the full path to the source file, this is avoided, however these links are more fragile. For example when syncing links the absolute path to the source is likely different on another device.<\/li><\/ol>\n<div class=\"syndication-links\"><\/div>","protected":false},"excerpt":{"rendered":"<p>I learned this week that relative symbolic links are relative to the link not the current working directory. What does this mean? To remind ourselves, the format of ln is: Example Say we have the following structure: Say we want to create a symlink to app\/config.yaml. If we are familar with the cp command we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"mf2_syndication":[],"webmentions_disabled_pings":false,"webmentions_disabled":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-362","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/posts\/362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/comments?post=362"}],"version-history":[{"count":7,"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/posts\/362\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/amasan.co.uk\/vandragt\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}