35

An URL link has been saved in *.url file via Windows. If you double click the file in Windows, it will open your default browser to the indicated URL. Here is an example of *.url file content:

[InternetShortcut]
URL=http://abdennour-insat.blogspot.com/

When the same file is copied on Ubuntu OS, Ubuntu handles it such as a text file. Hence, the browser does not open the URL when double-clicking the file.

My question is: what is the equivalent of *.url file in Linux to make shortcut file for URLs?

4
  • 3
    It's ridiculous that .url files still can't be opened by default. bugs.launchpad.net/ubuntu/+source/gnome-mime-data/+bug/185165
    – endolith
    Mar 31, 2020 at 16:57
  • Here is a small workaround saidulhassan.com/open-url-files-in-linux-mint-ubuntu-1029 but I hope this will be eventually supported by linix distros out of the box Jul 19, 2020 at 15:45
  • 3
    I tried to set Firefox to open .url files, and associating the file type inside Firefox as well, but a bizarre bug made it aggressively open tabs ad infinitum. That both scared me and made me laugh for a second.
    – geekley
    Aug 8, 2020 at 22:41
  • 1
    @mchid URL files aren't an app or an executable or a platform-specific "Windows shortcut". They're a generic file format for storing a URL. There's no reason to expect it to work on only one operating system. Should .txt files only open in Windows?
    – endolith
    Nov 1, 2022 at 14:28

5 Answers 5

27

In Ubuntu an URL shortcut is stored in a .desktop file as follow (for example):

[Desktop Entry]
Encoding=UTF-8
Name=Link to Best Practices Software engineering
Type=Link
URL=http://abdennour-insat.blogspot.com/
Icon=text-html

If you still want to open your Windows URL files in Ubuntu, here is described how you can do it:

The Perl script given in that article appears to be broken, but the following code should do the same thing correctly:

#!/usr/bin/perl
# Script to make Microsoft Windows Internet Shortcuts (*.url) work on Linux.

my $browser = 'sensible-browser';  # use the system default browser

while (<>) {
    # match any line of the form "URL = something-without-spaces"
    if (/^\s*URL\s*=\s*(\S+)\s*$/) {
        exec $browser, $1;         # successful exec never returns
        die "$0: could not launch $browser: $!\n";
    }
}
1
  • 3
    That Perl script looks broken in several ways: the "filter out carriage return" regexp won't work because it's missing a backslash, and indeed, the earlier regexp in the if clause will never match unless the URL contains the number 1, for the same reason. Also, if it does somehow manage to match, the URL will be passed unescaped to the shell, which is not only a security flaw, but will also break any URLs containing e.g. parameters separated with &. I've submitted an edit suggestion to your answer fixing these issues. Oct 5, 2013 at 11:40
16

In Unity we have .desktop files for defining items on the launcher, desktop, or other locations. To create these see the following question:

A link to an internet file may have an entry as simple as the following

[Desktop Entry]
Encoding=UTF-8
Name=Internet Link
Type=Link
URL=<url>
Icon=<icon to display>

You can create such a file with a text editor, or much easier by simply dragging and dropping a bookmark from your browser to the desktop.

However keep in mind that such a simple .desktop file will not automatically open the given URL in a browser if the target is e.g a text document, or image. To overcome this see the following question:

In case we do need to open .URL files more often (e.g. from a shared drive) we may also run a bash script similar to this to open them:

#! /bin/bash

# opens Windows URL file submitted as command line argument in browser

source $1
xdg-open $URL

The command not found error from this script can be ignored or sent to /dev/null. If we must then we could associate this script to a Mime type for the extension URL to double click open an URL file.

2
  • 1
    An amazingly simple script when compared to the other answer. Put that into a .desktop file and associate .url files with it then you are done.
    – kiri
    Oct 10, 2013 at 10:06
  • 4
    Simple, but an attack vector. As we are executing/running the first argument (the url file). Jul 9, 2015 at 6:42
5

You can make your Linux file manager open .URL file in your default browser. This is particularly useful for people who share files between Windows and *nix machines. To do this create a script file let's call it mswin-urlfile (or what ever you like) in /usr/local/bin (or your preferred folder). Change permission: chmod +x /usr/local/bin/mswin-urlfile

#!/usr/bin/bash
if [ "$1" == "" ] || [ "$1" == "--help" ] ; then
  echo "$(basename "$0") URLFILE - opens a .url file in the default browser"
else
  sed 's/^BASEURL=/URL=/' "$1" | grep -m 1 '^URL=' | sed 's/^URL=//' | sed 's/\r//' | xargs xdg-open
fi

To test use it manually from a shell prompt using:

mswin-urlfile test.url

Next you will need to configure file manager to call mswin-urlfile when opening a .URL file type. This works across all *nix and shells allowing you to open your .URL files from within your file manager. Exactly how to do this depends on the file manager you use. Lookup "file association" for the specific file manager you use.

For example Nautilus file manager is mentioned at 369967 and create a mimetype for the .url extension

Open Applications > System Tools > File Types Editor, and click the New button. Enter the following information in the corresponding tabs:

► General: Category: Text and source code

Name: x-url

Description: Microsoft Internet Explorer Shortcut

You can choose an icon for the .url file-type via the browse button […] – if you don’t have any, there are some at the bottom of this post that you can save to a folder like /home/yourusername/Settings/Icons.

► Filenames:

Filename pattern: *.url

► File contents:

When you click +Add, you’ll see more than one data entry field, but all you need to worry about is the Value: one.

Value: [InternetShortcut]

Now your system knows what .url files are, but it still doesn’t know what to do with them. You now have to associate the .url extension with the executable script fx-url, so right-click any .url file and go to Properties > Open With. Click the Add button, and at the bottom of the “Add Application“ window you’ll see the “Use a custom command“ option; click this, then either browse to /user/bin and select the symlink you created, or enter /usr/bin/Web Shortcut Browser (or the appropriate name if you changed it). Click the Add button to save your changes (but leave the “Properties“ window open as you’ll need it in the next step).

Now you need to make the associated action the default option for double-clicking, otherwise you’ll have to right-click .url files and choose the required option from the context menu. To make opening in Firefox the default action, in the Open With tab of the “Properties“ window, click the dot to the left of the entry you just added (eg: “Web Shortcut Browser“), then click Close.

open any Nautilus (file manager) window and go to Edit > Preferences > Behaviour. In the “Executable Text Files“ section, make sure that “View executable text files when they are opened“ is selected. Click Close, and it is done (you may need to log out or reboot for the changes to take effect). Now when you right-click any .url file, you should see “Open with Web Shortcut Browser“ as the top entry of the context menu, and it will be the default action for double-clicks.

I use a different file manager so I have not tested the above Nautilus instructions.

I prefer to keep using URL files as I use a mixed environment Android + app to open or create URL files; Linux + script (above) to open URL files; Windows (built in URL file support). Alternatively, you could convert every system to just use bookmark files and sync them, but builtin browser syncing has messed up my bookmarks more than once. If you use different browsers (eg Chrome, Firefox, Brave) on different system syncing is not simple.

0

I'm on Lubuntu 18.04 and using a .desktop file with Type=Link and URL didn't work for me. It only shows an error message saying: HTTP Error: Bad Request.

As a workaround, I call my web browser directly with the URL:

[Desktop Entry]
Encoding=UTF-8
Name=My favorite website
Type=Application
Exec=firefox https://url.com

The problem seems to be with the file manager, in my case PCManFM 1.2.5. It throws a segfault in libfm.so.4.0, as can be seen via dmesg.

0

You might want to add this command to your file manager's default applications for .url files.
(Replace chromium with your favorite browser)

bash -c "a=$(cat %f | grep URL=); chromium ${a:4};"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .