I have a local directory and want to share a clickable link that will open the user's default file browser.

How to generate an URL of the file:// protocol for a given local filename?

some_program /foo/bar    # file:///foo/bar
some_program "/föö bar"    # file:///f%C3%B6%C3%B6%20bar
some_program "$( realpath . )"    # link to local directory

I found some hints for the reverse operation utilizing naive replacement commands in sed what fails for filenames with non-alphanumeric characters like whitespace.


The Perl module URI::file supports this:

$ perl -MURI::file -E'say URI::file->new($_) for @ARGV' /foo/bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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