convert relative symbolic links to absolute symbolic links

后端 未结 2 2031
情深已故
情深已故 2021-02-08 03:52

How can I convert relative symbolic links to absolute symbolic links recursively in bash?

相关标签:
2条回答
  • 2021-02-08 04:32

    Just in case somebody looking for reverse operation: conversion from absolute links to relative:

    Using python:

    find . -lname "`pwd`*" -exec sh -c 'ln -snvf `python -c "from os.path import *; print relpath(\"$(readlink {})\",dirname(\"{}\"))"` {}' \;
    

    Using only bash and sed:

    find . -lname "`pwd`/*" -depth 1 -exec sh -c 'ln -snvf `echo $(readlink {}) | sed "s|\`pwd\`|.|"` {}' \;
    find . -lname "`pwd`/*" -depth 2 -exec sh -c 'ln -snvf `echo $(readlink {}) | sed "s|\`pwd\`|..|"` {}' \;
    find . -lname "`pwd`/*" -depth 3 -exec sh -c 'ln -snvf `echo $(readlink {}) | sed "s|\`pwd\`|../..|"` {}' \;
    etc....
    

    This would convert all absolute links in the subdirectories to a relative ones.

    0 讨论(0)
  • 2021-02-08 04:49
    ln -sf "$(readlink -f "$link")" "$link"
    
    0 讨论(0)
提交回复
热议问题