Is there some native function(shell, linux command) to merge/compute the full path?
example:
old_path="~/test1/test2/../dir3//file.txt"
new_path=FUN($old_path)
echo "$new_path" // I want get this "/home/user/test1/dir3/file.txt"
Is there some native function(shell, linux command) to merge/compute the full path?
example:
old_path="~/test1/test2/../dir3//file.txt"
new_path=FUN($old_path)
echo "$new_path" // I want get this "/home/user/test1/dir3/file.txt"
Does
new_path=$(eval cd "$old_path"; pwd)
work for you? You can also use pwd -P
if you want symlinks resolved. You can make life easier if you use $HOME
instead of ~
in old_path
. Then you don't need the eval
.
Use readlink
:
$ readlink -m ~/foo.txt
/home/user/foo.txt
$ readlink -m ~/somedir/..foo.txt
/home/user/foo.txt
It also handles symlinks.