![絶対パスを相対パスに変換するには? [コピー]](https://linux33.com/image/154545/%E7%B5%B6%E5%AF%BE%E3%83%91%E3%82%B9%E3%82%92%E7%9B%B8%E5%AF%BE%E3%83%91%E3%82%B9%E3%81%AB%E5%A4%89%E6%8F%9B%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%EF%BC%9F%20%5B%E3%82%B3%E3%83%94%E3%83%BC%5D.png)
コマンドを使用して絶対パスを現在の作業ディレクトリへの相対パスに変換するにはどうすればよいですか?
答え1
答え2
ただ「自分でやる」楽しみのために
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
そして
here=/dir1/dir2/dir3
../../dir4/dir5/my.file