Re: Determine path of file



On Mon, 4 Feb 2008 08:04:03 -0800 (PST), Kenneth Brun Nielsen wrote:
I reference a file from a script through command line parameters.

I.e.
script.sh [referenced file]

The file can be refered to as both absolute and relative path. How can
I easily get the complete (absolute) path independent on whether it is
specified absolute or relative.

I tried with addition of $pwd, but in case of an absolute reference I
run into problems with two consecutive slashes, e.g. /home/kbn/path//
home/kbn/path/file

At the moment I use sed, but I assume some more easy/pretty solutions
exist(?)
[...]

That is a FAQ.

you could try:

file=$1
dir=$(dirname -- "$file")
base=$(basename -- "$file")
full_dir=$(cd -P -- "$dir" && pwd -P) || exit
full_file=$full_dir/$base

Note that $full_dir will be exempt of any symbolic link in its
components, but $full_file may still be a symbolic link.

On some systems, you'll find a non-standard readlink command
that you can use as:

full_file=$(readlink -f -- "$file") || exit

to obtain an absolute path free of symlinks.

--
Stephane
.



Relevant Pages

  • Re: The Real TWINS Paradox - the Simplest Version
    ... then A's AVERAGE absolute velocity is also at least ... there is no absolute reference. ... According to the transorm these poles will be length contracted ... addition to the contraction of the poles, ...
    (sci.physics.relativity)
  • Re: Fastest Time
    ... >> it's also irrelevant to SR's claim that ranking by absolute ... >> is done in one particular reference frame, ... > Because my model agrees with scientific research. ...
    (sci.physics)
  • Re: Fastest Time
    ... >> it's also irrelevant to SR's claim that ranking by absolute ... >> is done in one particular reference frame, ... > Because my model agrees with scientific research. ...
    (sci.physics.relativity)
  • Re: Is "malfunctioning" absolute or relative?
    ... it depends on the reference ... "Fartherness" is not absolute. ... Strong proponents for that explanation were Lorentz (who laid ... Physics can be done ...
    (sci.physics.relativity)
  • Re: Is "malfunctioning" absolute or relative?
    ... it depends on the reference ... "Fartherness" is not absolute. ... Chicago is farther than ... explanation were Lorentz, ...
    (sci.physics.relativity)