{"id":393,"date":"2026-11-22T14:20:09","date_gmt":"2026-11-22T13:20:09","guid":{"rendered":"https:\/\/www.netlabor.it\/?p=393"},"modified":"2026-11-22T22:53:32","modified_gmt":"2026-11-22T21:53:32","slug":"da-link-assoluti-a-link-relativi-anatomia-di-uno-script-bash","status":"publish","type":"post","link":"https:\/\/www.netlabor.it\/en\/da-link-assoluti-a-link-relativi-anatomia-di-uno-script-bash\/","title":{"rendered":"From absolute to relative links: anatomy of a Bash script"},"content":{"rendered":"\n<div class=\"wp-block-bogo-language-switcher\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You moved a project to another disk. Suddenly, dozens of symlinks pointing to <code>\/home\/you\/old-archive\/...<\/code> are broken. Or you cloned a repository that used absolute links, and now nothing works on another machine. The solution is to convert them to <strong>relative<\/strong> links, so the project becomes portable. This script does exactly that: <strong>replaces, in place, symbolic links with an absolute target with equivalent links whose target is relative<\/strong>, calculated with respect to the directory containing the link.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s look at why it&#8217;s built this way and which design choices actually matter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The starting problem<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An absolute symlink contains a path starting from the root:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">project\/data -&gt; \/home\/you\/project\/real-data<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It works as long as the project stays where it is. If you move it to another disk, share it with a colleague, or clone it on another machine, the link breaks: <code>\/home\/you\/project\/real-data<\/code> no longer exists. A <strong>relative<\/strong> link instead contains a path calculated relative to the link&#8217;s own position:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">project\/data -&gt; real-data<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This link works anywhere, as long as the project&#8217;s internal structure stays intact. The conversion isn&#8217;t hard in theory, but doing it by hand on dozens of links is tedious and dangerous: one mistake and you lose the original link.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The script automates the conversion safely.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The design choices that make the difference<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Only absolute links, only links<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The script is surgical: it touches <strong>only<\/strong> symbolic links pointing to an absolute path. Everything else is handled with a clear message:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Path that isn&#8217;t a link<\/strong>: skipped with a warning, not an error.<\/li>\n\n\n\n<li><strong>Link already relative<\/strong>: skipped, nothing to do.<\/li>\n\n\n\n<li><strong>Broken link<\/strong> (missing target): <strong>still converted<\/strong>. This is deliberate: a broken link with an absolute path is even more fragile than a working one, because there&#8217;s no way to know where it pointed. Converting it to relative preserves the information &#8220;this link points to something in the same structure&#8221;, even if the target is missing.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This distinction between &#8220;skipped&#8221; and &#8220;failed&#8221; is important: skipping a file that isn&#8217;t a link isn&#8217;t a failure, it&#8217;s the correct answer to the question &#8220;is this file an absolute link?&#8221;. Only real errors (readlink failed, ln failed, mv failed) increment the <code>FAILED<\/code> counter.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Strips the trailing slash, except on root<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">while [[ \"$link\" == *\/ &amp;&amp; \"$link\" != \"\/\" ]]; do\n    link=\"${link%\/}\"\ndone<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A path like <code>project\/link\/<\/code> with a trailing slash is resolved by the shell <strong>through<\/strong> the link, not tested as a link. If you pass <code>-L project\/link\/<\/code> to <code>test<\/code>, the shell first resolves the link and then tests the target, getting &#8220;not a link&#8221;. Stripping the trailing slash avoids this trap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The special case for <code>\/<\/code> is because <code>\/\/<\/code> and <code>\/<\/code> are the same thing, but <code>\/\/\/<\/code> isn&#8217;t: the root must never be touched.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Creates the new link with a temporary name, then atomically replaces<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This is the heart of safety:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">TMP_LINK=\"$dir\/.a2r-tmp-$$\"\nln -sr -- \"$target\" \"$TMP_LINK\"\nmv -Tf -- \"$TMP_LINK\" \"$dir\/$base\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The flow is:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Create a new link with a temporary name in the <strong>same directory<\/strong> as the original link.<\/li>\n\n\n\n<li>If <code>ln<\/code> fails, the original link is still intact.<\/li>\n\n\n\n<li>If <code>ln<\/code> succeeds, <code>mv -Tf<\/code> atomically replaces the original link with the new one.<\/li>\n\n\n\n<li>If <code>mv<\/code> fails, the temporary file is cleaned up and the original link remains.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The result: <strong>at no point is the original link missing or corrupted<\/strong>. If something goes wrong halfway, you&#8217;re back to the starting state. This is why the temporary file is in the same directory: <code>mv<\/code> across different directories isn&#8217;t atomic on all filesystems, whereas <code>mv<\/code> in the same directory is.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>.a2r-tmp-$$<\/code> prefix includes the PID, so two concurrent runs don&#8217;t step on each other&#8217;s toes.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. <code>ln -sr<\/code> does the calculation, not the script<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The relative path calculation is delegated to <code>ln -sr<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ln -sr -- \"$target\" \"$TMP_LINK\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>-r<\/code> (or <code>--relative<\/code>) option tells <code>ln<\/code> to compute the relative path <strong>with respect to the destination link&#8217;s position<\/strong>. This is much more robust than any manual calculation with <code>realpath --relative-to<\/code>, because it correctly handles:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Paths with <code>..<\/code> and <code>.<\/code> in the middle.<\/li>\n\n\n\n<li>Intermediate symlinks (with <code>-r<\/code> it follows them, not treats them as text).<\/li>\n\n\n\n<li>Edge cases like links at the root or in deep directories.<\/li>\n\n\n\n<li>Paths with spaces and special characters (thanks to <code>--<\/code> in front).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Delegating the calculation to <code>ln<\/code> also means the script doesn&#8217;t have to reimplement the relativization logic, which is surprisingly subtle to get right.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">5. Traps for interrupt, termination and errors<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Three distinct traps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>EXIT<\/code>: cleans up the temporary file if one is left over.<\/li>\n\n\n\n<li><code>INT<\/code> (Ctrl+C): cleans up and exits with 130.<\/li>\n\n\n\n<li><code>TERM<\/code>: cleans up and exits with 143.<\/li>\n\n\n\n<li><code>ERR<\/code>: cleans up, reports the exact line, and exits with 1.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Combined with <code>set -Eeuo pipefail<\/code>, this means <strong>pressing Ctrl+C at any moment never leaves a temporary file on disk<\/strong>. The original link is always intact, because the replacement only happens with an atomic <code>mv<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6. Handles names starting with a dash<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Argument parsing follows the POSIX convention:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for arg in \"$@\"; do\n    if [[ \"$END_OPTS\" == true ]]; then\n        POSITIONAL+=(\"$arg\")\n        continue\n    fi\n    case \"$arg\" in\n        -h|--help) usage; exit 0 ;;\n        --) END_OPTS=true ;;\n        -*) T unknown_option \"$arg\"; usage &gt;&amp;2; exit 2 ;;\n        *) POSITIONAL+=(\"$arg\") ;;\n    esac\ndone<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A link called <code>-strange<\/code> would be interpreted as an option without <code>--<\/code>. With <code>--<\/code> in front, it&#8217;s treated as a path. It&#8217;s the kind of detail that seems excessive until you hit a file with that name and realize that without <code>--<\/code> the script treats it as a flag.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">7. Dual language with POSIX precedence<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>detect_user_language<\/code> function follows POSIX precedence:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">language=\"${LC_ALL:-${LC_MESSAGES:-${LANG:-en_US}}}\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LC_ALL<\/code> wins over <code>LC_MESSAGES<\/code>, which wins over <code>LANG<\/code>. This is the correct behavior per the standard, and it means that if you set <code>LC_ALL=it_IT.UTF-8<\/code> you get Italian even if <code>LANG=en_US.UTF-8<\/code>. Only Italian and English are supported, with messages in a single <code>T<\/code> function with two <code>case<\/code> blocks.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8. Final summary and exit code<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">At the end, the script reports:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">=== RESULT ===\n\u2705 Converted: 42\n\u23ed  Skipped: 3\n\u274c Failed: 0\n\ud83c\udf89 Operation completed!<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The exit code is meaningful:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>0<\/strong> if everything was converted or skipped (no real errors).<\/li>\n\n\n\n<li><strong>1<\/strong> if there was at least one failure (readlink, ln or mv failed).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;Skipped&#8221; doesn&#8217;t contribute to the exit code, because it isn&#8217;t an error: it&#8217;s the correct answer to the question &#8220;is this file an absolute link?&#8221;. Only real failures count.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">9. <code>--help<\/code> documents the requirements<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The help text explicitly says <strong>&#8220;Requires GNU coreutils (ln -r, mv -T)&#8221;<\/strong>. This matters because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ln -r<\/code> doesn&#8217;t exist on BSD\/macOS (where <code>ln<\/code> lacks <code>--relative<\/code>).<\/li>\n\n\n\n<li><code>mv -T<\/code> is GNU-specific (on BSD, <code>mv<\/code> doesn&#8217;t have <code>-T<\/code>).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Whoever reads the help knows immediately whether the script will work on their system. Better an explicit error at the start than a silent failure halfway through.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">10. Pipeline usage with <code>find<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The help suggests a very useful pattern:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">find . -type l -exec .\/absolute2relative-links.sh {} +<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This finds all symlinks under the current directory and passes them in batch to the script. <code>{} +<\/code> groups multiple files into a single invocation (up to a length limit), reducing startup overhead. Combined with the script&#8217;s handling of difficult names, this pattern is safe even on large directory trees.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What it doesn&#8217;t do (and that&#8217;s fine)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Doesn&#8217;t follow links<\/strong>: it converts the link, not its target. If the target is itself an absolute link, you&#8217;ll have to run the script on that too (or use <code>find<\/code> with <code>-L<\/code> to follow links, but beware of loops).<\/li>\n\n\n\n<li><strong>Isn&#8217;t portable to BSD\/macOS<\/strong>: requires GNU coreutils. On macOS, install <code>coreutils<\/code> with Homebrew and use <code>gln<\/code>\/<code>gmv<\/code> with <code>--relative<\/code> (but the argument syntax may vary).<\/li>\n\n\n\n<li><strong>Doesn&#8217;t create backups<\/strong>: converts in place, atomically. If you want a backup, do it first with <code>cp -a<\/code> or similar. The original link is still recoverable while the conversion is in progress, but not after.<\/li>\n\n\n\n<li><strong>Doesn&#8217;t recurse<\/strong>: converts only the links you pass it. For large trees, use <code>find<\/code> as suggested in the help.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How to use it<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Make sure you have GNU coreutils (on Linux this is almost always the case; on macOS <code>brew install coreutils<\/code>).<\/li>\n\n\n\n<li>Save the script as <code>absolute2relative-links.sh<\/code> and make it executable: <code>chmod +x absolute2relative-links.sh<\/code>.<\/li>\n\n\n\n<li>Run on specific links:bash.\/absolute2relative-links.sh project\/data project\/log<\/li>\n\n\n\n<li>Or on all links in a tree:bashfind . -type l -exec .\/absolute2relative-links.sh {} +<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">In summary<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This script isn&#8217;t complicated, it&#8217;s <strong>surgical<\/strong>. It touches only what it must touch, replaces atomically, cleans up after interrupts, and distinguishes between &#8220;skipped&#8221; (correct answer) and &#8220;failed&#8221; (real error). It does one thing, but it does it in a way that lets you trust running it on a production repository without fear.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you use it, read it first. If you modify it, keep the golden rule: <strong>the original link must never be missing or corrupted, not even for an instant<\/strong>. Everything else is negotiable.<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-8ed60c7b-48c1-4c24-aefb-46049b4bbcc4\" href=\"https:\/\/www.netlabor.it\/netlabor\/wp-content\/uploads\/2026\/11\/absolute2relative-links.sh_.zip\">Absolute to Relative Links<\/a><a href=\"https:\/\/www.netlabor.it\/netlabor\/wp-content\/uploads\/2026\/11\/absolute2relative-links.sh_.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-8ed60c7b-48c1-4c24-aefb-46049b4bbcc4\">Download<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You moved a project to another disk. Suddenly, dozens of symlinks pointing to \/home\/you\/old-archive\/&#8230; are broken. Or you cloned a repository that used absolute links, and now nothing works on another machine. The solution is to convert them to relative links, so the project becomes portable. This script does exactly that: replaces, in place, symbolic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"full-width-page-template.php","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/www.netlabor.it\/?p=391","footnotes":""},"categories":[6,7],"tags":[8,9],"class_list":["post-393","post","type-post","status-publish","format-standard","hentry","category-informatica","category-programmazione","tag-informatica","tag-programmazione","en-US","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts\/393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/comments?post=393"}],"version-history":[{"count":2,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/posts\/393\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netlabor.it\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}