Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get full path of a file

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
Hi unix gurus,
Is there a way to get the complete path of a file.

ie say my pwd is /root/mydir

>> pwd
>> /root/mydir
>> ls ../myfile.xls

is there any function that you take ../myfile.xls as input and return /root ?
Any suggestions in the right direction are welcome

Thanks
Philipose
 
Er... It's not quite as simple as that. The example given is ../myfile.xls and .. is the parent directory, not the current directory. The parent directory should be replaced by $(dirname $(pwd))

Try
Code:
#!/bin/ksh

[[ $# -ne 1 ]] && { echo No file name; exit; }

echo $1 | sed 's!\.\.!'$(dirname $(pwd))'!' | sed 's!\.!'$(pwd)'!'
The first sed expression, using ! as separator, replaces .. with the parent directory, the second replaces . with the current directory. sed gurus could probably make this simpler and quicker

Ceci n'est pas une signature
Columb Healy
 
columb,
thanks for the suggestion. i need a little more info from this particular absolute path finder.

>> pwd
>> /root/mydir
>> ls ../myfile.xls
f(../myfile.xls) should return /root
f(./myfile.xls) should return /root/mydir
f(~/myfile.xls) should return /myhome
f(/root/mydir/myfile.xls) should return /root/mydir
f(./../../myfile.xls) should return /

thanks
philipose
 
That's a lot more complex

Effectively the routine should identify and replace . .. and ~ anywhere in the file path and replace it with the absolute path.

So, in pseudo code
Code:
start at current workign directory
for each part of supplied path
  amend directory
return path
And in ksh
Code:
#!/bin/ksh -xv

# Check for passed path
[[ $# -eq 1 ]] || { echo no param; exit 1; }
#If passed path starts with / it's absolute, otherwise relative
expr $1 : "/.*" > /dev/null && ret_path="" || ret_path=$(pwd)

#split path into sections separated by /
for bit in $(dirname $1 | tr '/' ' ')
do
  # if .. move up a level
  [[ $bit = '..' ]] && { ret_path=$(dirname $ret_path); continue; }
  # if . nfa
  [[ $bit = '.' ]] && continue
  # else add to path
  ret_path=${ret_path}/${bit}
done
 
echo $ret_path

This code has a bug in it. It doesn't deal gracefully with, from your example, ../../root/mydir which is a valid path.

Ceci n'est pas une signature
Columb Healy
 
Thanks Columb for your suggestion. I will try it out. Another thought came to me. I think the combining the basename command and the sed command might give the answer I am looking for. I think the basename command will take care of all ., ~, and .. and return absolute paths. I have to try it out yet.
thanks again
philipose
 
I got things working the below way. Thanks very much for your help. The variable abspath should contain the absolute path after running the code

# Get filename with extension of file
filename=`/usr/bin/basename $file`

# Get present working directory
cpath="$PWD"
# Get path (this could be relative path) of file
path=`/usr/bin/echo $file | /usr/bin/sed -e 's/filename$//'`

# Get absolute path of the file to be archived
usr/bin/cd "$path"
abspath=$PWD
usr/bin/cd $cpath
 
minor modification

# Get filename with extension of file
filename=`/usr/bin/basename $file`

# Get present working directory
cpath="$PWD"
# Get path (this could be relative path) of file
path=`/usr/bin/echo $file | /usr/bin/sed -e 's/filename$//'`

# Get absolute path of the file to be archived
cd "$path"
abspath=$PWD
cd $cpath
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top