4

Let's say I have a text file my-text-file.txt and the file is in the ~/Documents directory. How can I create a desktop shortcut to that file?

In other words, I want to have an icon on my desktop that by double-clicking on it will open and edit my-text-file.txt file located in the ~/Documents directory.

I"m running live Ubuntu USB with Persistent Storage, version 18.04.4 LTS.

1

3 Answers 3

5

Most text files are associated with a text file editor such as Gedit. This means that if you double click on such a file in the File Manager, the file will be opened in Gedit.

If that is the case in your system, you could enter this command:

ln -s ~/Documents/my-text-file.txt ~/Desktop/my-text-file.txt

It is called a symbolic link. This will give you an icon on the desktop which will, when clicked, open the file in your default text editor. It will still be the file in Documents though.

You can replace the second my-text-file.txt with anything you fancy: this only changes what it will say on your desktop. It doesn't even have to end in .txt.

5
  • 1
    Far easier solution. Editors (gedit and nano) appear to preserve the link when saving to it - this was not always the case in former times.
    – vanadium
    Jun 4, 2020 at 14:53
  • I wonder - now that I've created a symbolic link (using ln -s target source), is it possible to add it to the Favorites section?
    – BlueSkies
    Jun 4, 2020 at 19:45
  • 2
    You can also create a symbolic link on the desktop using the GUI. Open the directory with the file in GNOME Files (formerly known as Nautilus), drag the file's icon over the desktop, press <ALT>, release the mouse button, and select "Link Here". Given that OP talks about "double-clicking", they might prefer this method.
    – Matthew
    Jun 5, 2020 at 0:42
  • 1
    @Matthew: Is it possible to remove a symbolic link on Desktop using GUI?
    – BlueSkies
    Jun 5, 2020 at 18:54
  • 1
    Yes, just right-click and select "Move to Rubbish Bin" (in the en_gb localization; other Englishes may vary).
    – Matthew
    Jun 6, 2020 at 1:41
4

Create a .desktop file, and include the following Exec= line:

Exec=xdg-open /path/to/your/document

This will yield a launcher that when started, will open your document with the default application registered for that file type.

You can also explicity provide an application, as in

Exec=evince "/home/myuser/Documents/My Document.pdf"

When placing the launcher on your desktop in Ubuntu, you first need to right-click it and indicate that you allow execution of the launcher.

The minimum you need for a working launcher is a file containing:

[Desktop Entry]
Name=[Name that will appear in the menu]
Exec=[command to execute]
Type=Application

When you copy the file to ~/.local/share/applications it will show up in your launcher as well.

1

Besides symbolic links and custom launchers (i.e. application-type desktop files), there is another option, which is to use a link-type (type 2) desktop file with a file:// URL. The example-content package includes one; for your example, it would look like this:

[Desktop Entry]
Name=my-text-file.txt
Type=Link
URL=file:///home/my-user-name/Documents/my-text-file.txt
Icon=text-x-generic

(You will, of course, need to change my-user-name so that it matches the actual path of your home directory. The file:// URL scheme requires absolute paths.)

Name the file my-text-file.desktop (or anything else that ends with .desktop) and put it in ~/Desktop/. It will look like this:

Desktop showing Trash icon and link to my-text-file.txt

Double-clicking the shortcut will open the text file with your default text editor.

example text file open in Gedit

If you need to make any changes, you can edit the desktop file with a normal text editor. Just right-click and choose "Open With Other Application" and select your text editor of choice.

desktop file in Gedit

If you want a different icon, you can choose from many default options or provide a path to your image file of choice.

You must log in to answer this question.

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