26

Is there any way to create a symbolic link to a (HTTP) URL?

Update: The reason I want to do this is so that I can move this symlink to another computer without having to copy the file itself (it's big), and instead, the other computer will just use the online copy from the URL.

8 Answers8

17

It's not possible to create a symlink to an URL. If you can make executables and the target OS is Linux-alike, you can create a file which opens the URL as in:

#!/bin/sh
x-www-browser 'http://example.com/your/link'
Lekensteyn
  • 6,982
14

If you are using a GUI desktop in Linux, like Gnome or Unity, you can drag and drop a URL from Firefox and other browsers either onto the desktop or into a folder in the Nautilus file manager. This will create a .desktop file with several lines in it like this one:

[Desktop Entry]
Encoding=UTF-8
Name=Link to Google Calendar
Type=Link
URL=https://www.google.com/calendar/render?pli=1
Icon=text-html

As long as you are in the GUI, you can just double-click on the file to open it into the default Web browser. I do this in Ubuntu to store links to documents in my private Drupal wiki on my machine.

This may work for KDE, xfce, and other desktop managers, but I haven't tried it.

Tim Ingalls
  • 141
  • 1
  • 2
11

You want an automatic URL link, stored in a file in your file system, to open.

The way to do this is with a minimalist .HTML file. For example, to take you to the Google home page, place the following code in a file named Google.HTML:

<!DOCTYPE HTML>
<html>
  <head>
    <title>Google automatic redirect</title>
    <meta http-equiv="refresh" content="0; url=http://www.google.com/" />
  </head>
  <body>
    <h1>For older browsers, click Redirect</h1>
    <p><a href="http://www.google.com/">Redirect</a></p>
  </body>
</html>

When you open (i.e double click) on this file, the OS will open your default browser (e.g. Firefox) and render this little HTML file, which has a URL redirect in the header, which in turn will automatically open the URL in the redirect.

This can be adapted to take you to the online file as per your question.

The URL contains the protocol (e.g. HTTP), so just make sure that it's in there. To be more minimalist, you can omit the <title> and <h1> lines.

I tried the other answers on this page with Ubuntu 16.04 without success, but this solution works.

bad_coder
  • 649
wwmbes
  • 241
2

It's not possible to link to a HTTP location. You might be able to mount the location of this file via WebDAV to your system and link to the local mount, but this only works if it's configured to get exported via WebDAV..
But if you want to read the file (I think you are trying to do so), you anyhow have to download the content (even if it would be possible to create such a link). So I recommend to simply download it.

binfalse
  • 1,832
1

For ease of use, I wrote a script to generate bash-links like Lekensteyn suggested in the accepted answer. This is also able to properly handle arguments containing spaces or other special characters.

Making it runnable makes things even more handy. Run it like $ linkscript.sh "http://example.com/your/link" "YourLinkFile.sh". (Depending on your link and file name, this usually also works without the quotes ". As a rule of thumb, it is always safer with quotes.)

#!/bin/bash
printf '#!/bin/bash\nxdg-open %q\n' "$1" > "$2" && chmod +x -- "$2" #Overwrite existing file with > instead of appending with >> 
chmod +x $2 #Makes the generated script executeable

Thanks also for Lekensteyn's former comment suggesting the use of printf %q format specifier to ensure that special characters are properly quoted in case the URLs containing single quotes. This also simplified/shortened the code a little. The suggested script then uses the single printf line instead of the previous two-liner, which can be seen in the edit history. Thanks also to altermetax' comment for pointing to use xdg-open rather than x-www-browser to be compatible with most distributions.


Note about the shebang in the code:

You have to use #!/bin/bash instead of #!/bin/sh to get the correct version of printf and don't cause printf: %q: invalid directive as per this Q/A. Thanks to M.T for pointing out this improvement!

Cadoiz
  • 567
0

Just as in my first answer here, I wanted to provide a script to generate a html file as proposed by wwmbes. Just run it like $ html_linkscript.sh "http://example.com/your/link" "YourLinkFile.html". (Depending on your link and file name, this usually also works without the quotes ".)

#!/bin/bash
echo "<!DOCTYPE HTML>
<html>
  <head>
    <title>Automatic redirect to $1</title>
    <meta http-equiv=\"refresh\" content=\"0; url=$1\" />
  </head>
  <body>
    <h1>For older browsers, click Redirect</h1>
    <p><a href=\"$1\">Redirect</a></p>
  </body>
</html>" > "$2"
Cadoiz
  • 567
0

Just as in my first answer here, I wanted to provide yet a last script to generate a .desktop file as proposed by Tim Ingalls. I confess, that I figured out the basics but not details of generating a .desktop-file just about now. More Specification can be found here.

In order to run it, use this: $ desktop_linkscript.sh "http://example.com/your/link" "YourLinkFile.desktop". (Depending on your link and file name, this usually also works without the quotes ".)

#!/bin/bash
echo "#!/usr/bin/env xdg-open
[Desktop Entry]
URL=\"$1\"
Terminal=false
Type=Link
$3" > "$2"

The created .desktop-file is stripped to the (I believe mandatory) bare minimum lines. You can also add more configuration with a call like this: $ desktop_linkscript.sh "http://example.com/your/link" "YourLinkFile.desktop" $"Name=My script\nComment=Test hello world script\nX-Created-By= your name". Using the $3-parameter (see above), the optional ones are appended, but don't forget the $ before $Name=My Script\n... - I didn't find a more elegant way yet to resolve the \ns.

You can also use .desktop-files for other stuff as e.g. executing a command as described in my answer there.

Cadoiz
  • 567
0

we're getting closer to being able to do this. closest thing I know of is https://github.com/kahing/goofys which lets you mount cloud buckets.