How to create a hyperlink file?

I know this question sounds too generic but I am unable to find an answer for it.

How can I create a link (soft/hard) file which when opened, redirect me to a website with http protocol?

One possible way I can think of is creating a lame shell script and making it executable which can make use of a browser to open a website. But isn't there a concept of a "hyperlink file"?


Add this to target.html:

<html>
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
</html>

Now if you run firefox target.html it will open example.com.


To clarify, there is no such thing as a "hyperlink file". You may have heard of symbolic and hard links, but those are simply a way to refer to a file on disk - they are not URLs. From man ln:

Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.


There are .desktop files:

$ cat ~/Desktop/Link.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Link to your site
URL=http://your-site-url.com
Icon=text-html

This is a possible answer when your solution needn't be command-line-based, but supposed to gain much comfort.

Of course the .desktop files don't have to be stored in ~/Desktop/. I just used this location since they might be used in that location mostly.


xdbg-open is default application for open anything, so you can create bash script like this:

#!/bin/bash
if which xdg-open > /dev/null
then
  xdg-open YOUR_URL
elif which gnome-open > /dev/null
then
  gnome-open YOUR_URL
fi

Replace YOUR_URL accordingly, save file and make it executable (chmod +x filename.sh)


One answer that has not come up yet, is using Linux’s binfmt_misc capabilities, to create a native executable link format that works on the kernel level!

To automatically open any .link files in xdg-open, put this inside an executable file at /etc/local.d/binfmt_misc.start, or any file your OS runs on startup:

#! /bin/sh
echo ':open-hyperlink:E::link::/usr/local/bin/open-hyperlink:' > /proc/sys/fs/binfmt_misc/register

and put the following into the /usr/local/bin/open-hyperlink executable:

#! /bin/sh
xdg-open "`cat "$1"`"

After that, you can just “run” .link files that are marked as executable via any means, and it will open the link in the browser. Command line, GUI double click, whatever you like.

$ echo 'http://superuser.com/questions/986527/how-to-create-a-hyperlink-file' > this-page.link
$ chmod +x this-page.link
$ ./this-page.link
[Browser opens…]

You can of course change the extension and file format how you like, provided you change the open-hyperlink script accordingly. Even Windows .lnk files!

Of course your kernel has to have that module available and enabled, for it to work. (I have it compiled in.)

Check out the documentation on binfmt_misc, as there are a lot more possibilities, e.g. with matching on a pattern instead of a file extension.