20

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.

Bazer Con
  • 13
  • 2
  • 2
    URL's have little to do with HTTP (assuming that's what's implied). That's why it's explicitly `http://` at the beginning. You need to be more specific. Wouldn't just mounting WebDAV do, using `davfs`, again assuming you're on Linux? – Daniel Beck Apr 29 '11 at 20:26
  • Consider using WebDAV. Otherwise, most systems allow the storing of URL references, e.g. `.url` files on Windows or `.webloc` files on Mac OS X. – Daniel Beck Apr 29 '11 at 20:44
  • Is there a way to have a file on my hard drive automatically kept up-to-date with a version that's on the web? (But not deleted or overwritten with garbage if the online version disappears or changes, of course.) Should that be a different question? – endolith Apr 06 '12 at 14:28
  • 1
    @NotMe If you just want to create a cross-platform internet shortcut (using an HTML page that automatically redirects to another page), then see here: http://superuser.com/questions/538089/cross-platform-internet-shortcut-files – Anderson Green Jan 19 '13 at 04:12
  • @anderson this is basically what was proposed [in this answer](https://superuser.com/a/1340296/1586840), is it? – Cadoiz Oct 25 '21 at 09:57

8 Answers8

16

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,546
  • 5
  • 27
  • 48
  • 1
    If your default browser is Firefox and your also have Chrome installed, Chrome might open the URL due to a Firefox bug with "alternatives" system in dpkg. https://bugzilla.mozilla.org/show_bug.cgi?id=1218174 – dotnetCarpenter Oct 25 '15 at 13:33
  • For ease of use, you can take this script to generate links like suggested: https://superuser.com/a/1524254/910769 – Cadoiz Jul 01 '20 at 18:05
  • 1
    It would be more proper to use `xdg-open` rather than `x-www-browser` to be compatible with most distributions. – altermetax Sep 27 '21 at 21:54
  • Hey, this answer is incorrect. You could potentially use an S3 bucket (which is accessible by a URL) and then mount the bucket to your file system using FUSE -- check out https://github.com/kahing/goofys -- I haven't tested it but I'm pretty sure this is one way of doing it. I'm still looking for the solution that allows me to symlink arbitrary URL/files – ProfessorManhattan Dec 17 '21 at 04:59
12

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
  • 121
  • 1
  • 2
  • You can consider [the script provided here](https://superuser.com/a/1682826/910769) to automatically generate such a `.desktop` file. They can also be used for other stuff as [e.g. executing a command as described in my answer there.](https://unix.stackexchange.com/a/674052/318461) – Cadoiz Oct 20 '21 at 15:41
8

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
  • 643
  • 1
  • 7
  • 16
wwmbes
  • 211
  • 2
  • 3
  • This is a better solution as it can be served, unlike a shell script. Most browsers will let you get away with just `` provided you use the extension .html – caduceus Oct 01 '20 at 11:32
  • You can consider [the script provided here](https://superuser.com/a/1682615/910769) to automatically generate such a html file :) – Cadoiz Oct 19 '21 at 16:16
  • You can also consider/compare [this answer](https://superuser.com/a/1031155/910769) which provides some other details. – Cadoiz Oct 25 '21 at 10:01
3

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,794
  • 14
  • 10
  • But if you download it and it changes, your downloaded copy is out of date. – endolith Apr 06 '12 at 14:29
  • -1 as it is possible as seen in other answers and the op stated that the reason he wants to do this is so that he could 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. – Cadoiz Oct 19 '21 at 16:26
  • @Cadoiz The solutions provided in other answers are not **links**. They are either shell scripts (running a web browser), HTML files that perform redirection (opened via web browser), or .desktop files (a GUI/file manager feature that opens a web browser). A **link**, in strict sense, to a HTTP URL cannot be created, as links are a filesystem feature and work only within filesystems. The OP clearly stated he wants a symlink. This is not possible. If he'd say "shortcut" (which is **not** strictly defined), then yes, all the other answers can be applied. – raj Oct 19 '21 at 17:46
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
  • 519
  • 3
  • 10
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
  • 519
  • 3
  • 10
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
  • 519
  • 3
  • 10
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.

ProfessorManhattan
  • 181
  • 1
  • 1
  • 5