convert relative symbolic links to absolute symbolic links

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

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

2条回答
  •  梦毁少年i
    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.

提交回复
热议问题