Wanted to open a file (.pdb file: protein structure format, never mind) by a double-click.

The "open with" is manually directed to execute "pymol" (a program that open the .pdb format) with Ubuntu Tweak.

However, I found that the working directory is $HOME.

As other files on the directory I opened the file is crucial I want to access them.

I guess that the "pymol" is executed on $HOME while the absolute path of the "pdb file" (double-clicked file) is passed as an argument.

I guess this is somewhat general problem and ask a question:

How can I let the system change the "working directory" to the "directory where I clicked the file" and execute an program?

  • How did you solved this? The answers shown here is not working for me Jun 5 at 5:02

Although this is years later, here's what worked for me:

Exec=bash -c 'cd "%k" && ~/path/to/your/file'

%k gives you the path of the directory of the launcher you just launched.

  • 1
    the %k did not work (nor %d) but I did need the bash -c part Jul 14 '16 at 19:01
  • 1
    Glad I can help indirectly. You need the bash -c part to define ~ correctly. You don't need bash -c if you use the direct /home/marc path.
    – TuxForLife
    Jul 16 '16 at 4:13
  • 1
    bash -c done the job. helped me a lot thanks :)
    – Muthu
    Jul 28 '16 at 7:16
  • 2
    The %k resolves to the the full path, including the desktop file, so cd "%k" won't work. But you can use cd "$(dirname %k)".
    – ivant
    Sep 27 '16 at 22:43
  • I am confused, why do you go in a directory using "%k" and then the command just after && is executing something relative to ~ ?
    – vdegenne
    Feb 3 '20 at 12:01

None of the above solution worked on my debian. Also %d is deprecated. See Desktop Entry Specification.

So I'm using the following bash script to run pymol. First you need to create the following bash script file and save it in /usr/local/bin/run_pymol.sh. I saved the file in /usr/local because it seems the xdg-open does not understand what is the HOME directory or so on.

#!/usr/bin/env bash
# change working directory
cd `dirname $1`
# run pymol
/usr/bin/env pymol $@

Second, give the executable permission to that file. Open a terminal and type the following commands to give a permission and to confirm.

% chmod +x /usr/local/bin/run_pymol.sh
% run_pymol.sh

Finally, modify your pymol.desktop file as the following and then your pymol will start in the directory where the first given file is located.

#!/usr/bin/env xdg-open
[Desktop Entry]
Name=PyMOL Molecular Graphics System
GenericName=Molecular Modeller
Comment=Model molecular structures and produce high-quality images of them
Exec=/usr/local/bin/run_pymol.sh %U

Hope it helps you guys :-)

  • 7
    I was able to put Path=/my/path and that worked just fine. Path - If entry is of type Application, the working directory to run the program in.
    – Ripster
    Oct 4 '15 at 16:48

I don't understand why solution up there so upvoted, it do absolute different thing. It changes working directory to those where .desktop file is, while question was about to change working directory to those with opened file(s).

My solution:

Exec=/bin/bash -c 'cd $(dirname $0) && micro $0'

Bear in mind that setting env variables for .desktop files is different, like so

Exec=env CPATH=../include /bin/bash -c 'cd $(dirname $0) && micro $0'

You can go to /usr/share/applications and find the proper file.desktop that is called whenever you open a specific file type. Then edit it and in the exec line add cd PATH && to the start of the line.


OK -

Exec=cd PATH &&  <application-name> %F

did not work for me as PATH is not defined to point to the path of %F. The only variables available in any applications desktop file are:

  • %f a single filename.
  • %F multiple filenames.
  • %u a single URL.
  • %U multiple URLs.
  • %d a single directory. Used in conjunction with %f to locate a file.
  • %D multiple directories. Used in conjunction with %F to locate files.
  • %n a single filename without a path.
  • %N multiple filenames without paths.
  • %k a URI or local filename of the location of the desktop file.
  • %v the name of the Device entry.

There it is: %d is the single directory you're looking for, as it doesn't make sense to change to multiple directories. So go to /usr/share/applications and edit the file application-name.desktop (in your case pymol.desktop); you have to have superuser privileges to do that. Then locate the line that starts with "Exec=". It probably reads

Exec=<application-name> %F

(In your case Exec=pymol %F). Change this line to

Exec=cd %d && <application-name> %F

Save that desktop file, and next time you click on a .pdb file your working directory should be the one this .pdb file is in, so pymol can see all of your files there.

Of course, you could also start pymol from the command line after having first navigated to the directory where your files are, but I guess you wanted a GUI solution, so you have to do a little editing :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.