Como converter o caminho absoluto em caminho relativo?

Como converter o caminho absoluto em caminho relativo?

Como uso um comando para converter um caminho absoluto em um caminho relativo ao diretório de trabalho atual?

Responder1

Usar

realpath --relative-to=. /absolute/path

Mais sobre issoaqui.

Responder2

Apenas pela diversão de 'rolar o seu próprio'

here=/dir1
there=/dir1/dir4/dir5/my.file
root=""
if [ ! -z $(grep "^$here" <<<$there) ]; then
    root="./"
else while [ -z $(grep  "^$here" <<<$there) ]; do
    here=${here%/*}
    root=${root}../
    done
fi
echo $root$(sed "s|^$here/||g" <<<$there)

./dir4/dir5/my.file

e para

here=/dir1/dir2/dir3

../../dir4/dir5/my.file

informação relacionada