Say I have the paths
a/b/c/d/e/f
a/b/c/d
How do I get the below?
e/f
1) PATH and Path are the same since Windows environment variables are case insensitive (File paths in Windows environment not case sensitive?). 2) Windows use Path to locate executables that are not located in the "current folder".
Relative path is defined as path related to the present working directory(pwd).
An Absolute Path is a full path specifying the location of a file or directory from the root directory or start of the actual filesystem. Example: /home/javatpoint/Desktop/CollegeStudent. An Absolute path of any directory always starts with a slash (/) representing the directory root.
Simple: realpath --relative-to=$absolute $current .
You can strip one string from the other with:
echo "${string1#"$string2"}"
See:
$ string1="a/b/c/d/e/f"
$ string2="a/b/c/d"
$ echo "${string1#"$string2"}"
/e/f
From man bash
-> Shell parameter expansion:
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted.
With spaces:
$ string1="hello/i am here/foo/bar"
$ string2="hello/i am here/foo"
$ echo "${string1#"$string2"}"
/bar
To "clean" multiple slashes, you can follow Roberto Reale's suggestion and canonicalize the paths with readlink -m
to allow comparison with strings with the same real path up:
$ string1="/a///b/c//d/e/f/"
$ readlink -m $string1
/a/b/c/d/e/f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With