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, 2021 at 5:02
  • 1
    Path=/my/path in the _.desktop file
    – alchemy
    Mar 26, 2022 at 1:00

7 Answers 7


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, 2016 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, 2016 at 4:13
  • 1
    bash -c done the job. helped me a lot thanks :)
    – Muthu
    Jul 28, 2016 at 7:16
  • 3
    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, 2016 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, 2020 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 :-)

  • 9
    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, 2015 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'

I'm not kidding, its this simple...
Add one new line:

Icon={path to icon}
MimeType={MimeType if it has one;}
Name[en_US]={Name in whatever language you want}

That should be the general format of .desktop files.

Now the line you want to add is:

Path=<Working Directory>

Every application you open with a .desktop file runs in a subshell, and the working directory by default is $HOME. However the Path= tells the subshell to work in a specific directory.

So after adding Path= to your .desktop shortcut, it will now act like that directory is the default working directory for whatever you're launching, without actually needing to change the default working directory.


Similar but not identical approach to @tuxForLife:

passing current dir as first parameter:

#!/usr/bin/env xdg-open
[Desktop Entry]
Exec=/usr/bin/bash /home/frank/bin/checkExtensionless.sh "%k"


I encountered that the pathcomes in single quotes if the filename has spaces '/somewhere/my stuff/' so I had to remove those, then cd into it. (builtin is just being overzealous to bypass any fancy cd wrappers in your .bashrc)

#!/usr/bin/env bash

prunedQuotes="$(echo $1 | sed -e "s/'//g")"
workdir="$(dirname "$prunedQuotes")"
builtin cd "$workdir"
<your actual script>

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 :-)

You must log in to answer this question.

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