Convert absolute path into relative path given a current directory using Bash

Using realpath from GNU coreutils 8.23 is the simplest, I think:

$ realpath --relative-to="$file1" "$file2"

For example:

$ realpath --relative-to=/usr/bin/nmap /tmp/testing
../../../tmp/testing

Leave a Comment