Re: Determine path of file
- From: Stephane Chazelas <stephane_chazelas@xxxxxxxx>
- Date: 04 Feb 2008 16:15:12 GMT
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
.
- Follow-Ups:
- Re: Determine path of file
- From: Kenneth Brun Nielsen
- Re: Determine path of file
- References:
- Determine path of file
- From: Kenneth Brun Nielsen
- Determine path of file
- Prev by Date: Determine path of file
- Next by Date: Re: cygwin cannot start zsh not found
- Previous by thread: Determine path of file
- Next by thread: Re: Determine path of file
- Index(es):
Relevant Pages
|