Quantcast
Channel: How do you normalize a file path in Bash? - Stack Overflow
Browsing all 25 articles
Browse latest View live

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

I needed a solution that would do all three: Work on a stock Mac. realpath and readlink -f are addons Resolve symlinks Have error handling None of the answers had both #1 and #2. I added #3 to save...

View Article



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

I know this is an ancient question. I'm still offering an alternative. Recently I met the same issue and found no existing and portable command to do that. So I wrote the following shell script which...

View Article

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

FILEPATH="file.txt" echo $(realpath $(dirname $FILEPATH))/$(basename $FILEPATH) This works even if the file doesn't exist. It does require the directory containing the file to exist.

View Article

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

Based on loveborg's excellent python snippet, I wrote this: #!/bin/sh # Version of readlink that follows links to the end; good for Mac OS X for file in "$@"; do while [ -h "$file" ]; do l=`readlink...

View Article

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

Old question, but there is much simpler way if you are dealing with full path names at the shell level: abspath="$( cd "$path" && pwd )" As the cd happens in a subshell it does not impact the...

View Article


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

A simple solution using node.js: #!/usr/bin/env node process.stdout.write(require('path').resolve(process.argv[2]));

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

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 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 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 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 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 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 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 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 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 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 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 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 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
Browsing all 25 articles
Browse latest View live




Latest Images