How do you normalize a file path in Bash?


Question

I want to transform /foo/bar/.. to /foo

Is there a bash command which does this?


Edit: in my practical case, the directory does exist.

1
179
9/25/2012 12:04:23 PM

Accepted Answer

if you're wanting to chomp part of a filename from the path, "dirname" and "basename" are your friends, and "realpath" is handy too.

dirname /foo/bar/baz 
# /foo/bar 
basename /foo/bar/baz
# baz
dirname $( dirname  /foo/bar/baz  ) 
# /foo 
realpath ../foo
# ../foo: No such file or directory
realpath /tmp/../tmp/../tmp
# /tmp

realpath alternatives

If realpath is not supported by your shell, you can try

readlink -f /path/here/.. 

Also

readlink -m /path/there/../../ 

Works the same as

realpath -s /path/here/../../

in that the path doesn't need to exist to be normalized.

172
8/7/2018 9:38:49 PM

I don't know if there is a direct bash command to do this, but I usually do

normalDir="`cd "${dirToNormalize}";pwd`"
echo "${normalDir}"

and it works well.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon