Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Whenever I execute a script by clicking on it in a file manager window it gets run with my home directory as it's working directory, not directory that has the script within it where I'm clicking.

This is a pain if the code needs to access files in the actual directory where the script is located.

Is there a way to change this behavior?

Example: is located in the folder /home/phil/src/python is also located there and is read by during execution.

If I open a terminal in that folder and run the script, it runs with /home/phil/src/python as it's working directory and it successfully finds and opens

However, if I open my file manager (pcman in my case, Lubuntu), navigate to /home/phil/src/python and double click to run it it will run with /home/phil as it's working directory. It won't find and it will fail.

After googling this a bit it seems this also happens with Nautilus.

share|improve this question

For bash scripts you can start the script with

cd "$(dirname "$0")"

or more sophisticated and safer versions of this. Probably in python you can do something similar (I don't know python), or you can wrap your script with a bash script.

share|improve this answer
Thanks. Yeah, there's definitely ways to do that sort of thing in Python, change the directory in the script. Probably the best way in the long term, especially for any code that's going to be released in anyway. I was kinda hoping there might be some way to tell the file manager to run a script in the current directory though, like an option / setting within the file manager. Just for ease of use while I'm writing the script. – MTLPhil Oct 20 '11 at 20:57
The best is probably for you to write the script so that it takes the data file as a parameter and that it can function correctly either without any other files than the one you pass as a parameter or have its files in a known lication. You can then associate files with your script or add it to the context menu or whatever. Much easier to use in the long run. :-) – Lakritsbollar Mar 3 '12 at 15:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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