-1

Is there a simple utility that will print a file's URL to the console? An example (where url is said utility), given a file index.html in /var/www/index.html:

user@server$ url index.html
file:///var/www/index.html
user@server$

If it makes a difference, I'm on Mac OS X, Mountain Lion.

1

3 Answers 3

2
 #!/bin/sh
 #/usr/local/bin/url
 echo file://`readlink -f $1`

The readlink command will traverse the path up to find whatever the full path to any specified file is. Put in a script, this is passing whatever argument you make to readlink and prepending it with "file://"

4
  • the readlink solution also happens to be relatively portable and shell-independent.
    – voretaq7
    Nov 28, 2012 at 23:10
  • Unfortunately, although this works fine on e.g. Ubuntu, this will not actually work on Mac OS X 10.8 (or 10.6 for that matter), where readlink is just an alternative, simplified invocation of stat. Rather, it results in the following error: readlink: illegal option -- f usage: readlink [-n] [file ...] Nov 29, 2012 at 7:14
  • See this man page. Nov 29, 2012 at 7:20
  • If you install the GNU tool greadlink e.g. using a package manager like Homebrew or MacPorts and then change the suggested shell script appropriately, that will work. However, this solution still doesn't take care of percent encoding for the URL, which, since spaces are common in OS X file and folder names, would be a good idea. Nov 29, 2012 at 7:30
0

You have several options to tackle this. From the command line you would do:

cd / 
find .|grep index.html|sed 's&./&file:///&1'

You will need to do this from a privileged account. If the file name changes you can script the above command to expand your searches fairly easily. Hope this helps.

0

do you need this?

alias url="echo  file://$(pwd)$1"
1
  • We generally prefer answers that have a little bit more detail than this (the "why", not just "do you need [insert command here]"). Please consider adding some detail to your answer. Thanks.
    – voretaq7
    Nov 28, 2012 at 21:39

Not the answer you're looking for? Browse other questions tagged .