![절대 경로를 상대 경로로 변환하는 방법은 무엇입니까? [복사]](https://linux55.com/image/154545/%EC%A0%88%EB%8C%80%20%EA%B2%BD%EB%A1%9C%EB%A5%BC%20%EC%83%81%EB%8C%80%20%EA%B2%BD%EB%A1%9C%EB%A1%9C%20%EB%B3%80%ED%99%98%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F%20%5B%EB%B3%B5%EC%82%AC%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