I feel stupid -- determine the full path to a file

Michael L Torrie torriem at chem.byu.edu
Thu Dec 6 12:05:05 MST 2007


Michael L Torrie wrote:
> I have a script that is passed a filename.  This filename could be
> absolute, it could be relative.  I need the script to be able to
> determine the pull path to this file.  I've read lots of hacks for doing
> this that involve ls, find, and pwd, but none of them do what I need.
> If the script was invoked like this,
> 
> foo ../bar

Here's my pure bash method of accomplishing this, if anyone cares. :)

if [ "`echo $path | cut -c1-1`" != "/" ]; then
        path=`pwd`/$path
fi

#step one: remove single . paths
path=`echo $path | sed -e 's!/\./!/!g'`

#step two: remove 'something/../' with ''
path=`echo $path | sed -e 's![^/]*/\.\./!!g'`

#step three: remove duplicate slashes
path=`echo $path | sed -e s'!//!/!g'`


> 
> then I need foo to be able to get a path that's the real, reduced path,
> without any dots in it.  In other words, `pwd`/../bar is not going to
> work.
> 
> Any tips?
> 
> thanks.


-- 
Michael Torrie
Assistant CSR, System Administrator
Chemistry and Biochemistry Department
Brigham Young University
Provo, UT 84602
+1.801.422.5771




More information about the PLUG mailing list