I wonder how it would be possible to use a regular expression to simplify double dots from a file path (the path may not actually exist) ?
For example change /my/path/to/.././my/./../../file.txt
into /my/file.txt
or path/./to/../../../file.txt
into ../file.txt
.
Is it possible to do this in one command in bash ? (using sed
for example, not a complicated python or perl script)
edit: I came across this question but realpath
isn't available on the computer I use.
edit:
From F.J 's solution, I ended up building the following regex which works in more general cases (does not work if some folder of the path is named ....
):
sed -e 's|/\./|/|g' -e ':a' -e 's|\.\./\.\./|../..../|g' -e 's|^[^/]*/\.\.\/||' -e 't a' -e 's|/[^/]*/\.\.\/|/|' -e 't a' -e 's|\.\.\.\./|../|g' -e 't a'