Quantcast
Channel: How do you normalize a file path in Bash? - Stack Overflow
Browsing index pages (26 articles)

Answer by Peter L for How do you normalize a file path in Bash?

For absolute, normalized, potentially-missing path I used:"/$(realpath -m --relative-to / SOME_PATH)"# exampleecho "/$(realpath -m --relative-to / /etc/bogus/..)"There are more options you can see with...

View Article


Answer by Fabian Lehmann for How do you normalize a file path in Bash?

Since none of the presented solutions worked for me, in the case where a file does not exist, I implemented my idea.The solution of André Anjos had the problem that paths beginning with ../../ were...

View Article


Answer by David Pi for How do you normalize a file path in Bash?

If you just want to normalize a path, existed or not existed, without touching the file system, without resolving any links, and without external utils, here is a pure Bash function translated from...

View Article

Answer by Jeffrey Cash for How do you normalize a file path in Bash?

I made a builtin-only function to handle this with a focus on highest possible performance (for fun). It does not resolve symlinks, so it is basically the same as realpath -sm.## A bash-only mimic of...

View Article

How do you normalize a file path in Bash?

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.

View Article


Answer by Adam Liss for How do you normalize a file path in Bash?

Try realpath. Below is the source in its entirety, hereby donated to the public domain. // realpath.c: display the absolute path to a file or directory. // Adam Liss, August, 2007 // This program is...

View Article

Answer by Tim Whitcomb for How do you normalize a file path in Bash?

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.

View Article

Answer by Kent Fredric for How do you normalize a file path in Bash?

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...

View Article


Answer by alhernau for How do you normalize a file path in Bash?

Talkative, and a bit late answer. I need to write one since I'm stuck on older RHEL4/5. I handles absolute and relative links, and simplifies //, /./ and somedir/../ entries. test -x /usr/bin/readlink...

View Article


Answer by mattalxndr for How do you normalize a file path in Bash?

Use the readlink utility from the coreutils package. MY_PATH=$(readlink -f "$0")

View Article

Answer by loevborg for How do you normalize a file path in Bash?

A portable and reliable solution is to use python, which is preinstalled pretty much everywhere (including Darwin). You have two options: abspath returns an absolute path but does not resolve symlinks:...

View Article

Answer by Jeet for How do you normalize a file path in Bash?

As Adam Liss noted realpath is not bundled with every distribution. Which is a shame, because it is the best solution. The provided source code is great, and I will probably start using it now. Here is...

View Article

Answer by schmunk for How do you normalize a file path in Bash?

My recent solution was: pushd foo/bar/.. dir=`pwd` popd Based on the answer of Tim Whitcomb.

View Article


Answer by Jesse Glick for How do you normalize a file path in Bash?

Not exactly an answer but perhaps a follow-up question (original question was not explicit): readlink is fine if you actually want to follow symlinks. But there is also a use case for merely...

View Article

Answer by apottere for How do you normalize a file path in Bash?

I'm late to the party, but this is the solution I've crafted after reading a bunch of threads like this: resolve_dir() { (builtin cd `dirname "${1/#~/$HOME}"`'/'`basename "${1/#~/$HOME}"`...

View Article


Answer by AsymLabs for How do you normalize a file path in Bash?

Try our new Bash library product realpath-lib that we have placed on GitHub for free and unencumbered use. It's thoroughly documented and makes a great learning tool. It resolves local, relative and...

View Article

Answer by Craig for How do you normalize a file path in Bash?

readlink is the bash standard for obtaining the absolute path. It also has the advantage of returning empty strings if paths or a path doesn't exist (given the flags to do so). To get the absolute path...

View Article


Answer by coldlogic for How do you normalize a file path in Bash?

I discovered today that you can use the stat command to resolve paths. So for a directory like "~/Documents": You can run this: stat -f %N ~/Documents To get the full path: /Users/me/Documents For...

View Article

Answer by André Anjos for How do you normalize a file path in Bash?

The problem with realpath is that it is not available on BSD (or OSX for that matter). Here is a simple recipe extracted from a rather old (2009) article from Linux Journal, that is quite portable:...

View Article

Answer by ϹοδεMεδιϲ for How do you normalize a file path in Bash?

Based on @Andre's answer, I might have a slightly better version, in case someone is after a loop-free, completely string-manipulation based solution. It is also useful for those who don't want to...

View Article
Browsing index pages (26 articles)


Latest Images