Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

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 professional 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 superuser.com... where it turns out it's already answered: superuser.com/questions/205127/… –  Nathan Nov 29 '12 at 20:55

3 Answers 3

up vote 2 down vote accepted
 #!/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://"

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.