Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

closed as off topic by mdpc, Magellan, John Gardeniers, Ward, voretaq7 Nov 29 '12 at 17:19

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Apologies... the question is more appropriate for where it turns out it's already answered:… – Nathan Nov 29 '12 at 20:55
up vote 2 down vote accepted
 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://"

share|improve this answer
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.

share|improve this answer

do you need this?

alias url="echo  file://$(pwd)$1"
share|improve this answer
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.