1

I need a way of getting the fullpath name of a file on a linux shell script. The full path may already be supplied or a relative file may be supplied.

afile.txt 
/home/me/bfile.txt

to

/home/me/afile.txt
/home/me/bfile.txt

any ideas?

eumiro
  • 207,213
  • 34
  • 299
  • 261
Darr
  • 11
  • 2
  • possible duplicate of [How can I list files with their absolute path in linux?](http://stackoverflow.com/questions/246215/how-can-i-list-files-with-their-absolute-path-in-linux) – eumiro Feb 03 '11 at 14:39

3 Answers3

7

Use readlink(1).

readlink -f afile
thevilledev
  • 2,367
  • 1
  • 15
  • 19
0

Quick hack:

get_fn()
{
  echo $(cd $(dirname $1); pwd)/$(basename $1)
}

But it can be costly.

0

If the directory will be the same, you can list the files in that directory in this way:

DIRECTORY=/some/directory
FILE_NAME="my-file-list"

for i in `ls -1 $DIRECTORY`
 do 
   echo $i >> $FILE_NAME
done

Otherwise, you would use the FIND command in the How can I list files with their absolute path in linux?

Community
  • 1
  • 1
Vinnie
  • 3,889
  • 1
  • 26
  • 29