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

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

 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://"

  • the readlink solution also happens to be relatively portable and shell-independent. – voretaq7 Nov 28 '12 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 ...] – Joe Carroll Nov 29 '12 at 7:14
  • See this man page. – Joe Carroll Nov 29 '12 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. – Joe Carroll Nov 29 '12 at 7:30

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.


do you need this?

alias url="echo  file://$(pwd)$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 '12 at 21:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.