How can I have a script detect where it's being run from in a graphical file manager? For instance, I want to run a Windows program in a directory with Wine by double clicking it in Nautilus, but Wine fails to load the DLL files because it isn't treating the file manager's open folder as the current working directory. It assumes it's being run from my home directory.

Edit for clarity:

In this example I'm not trying to detect where the wine executable I'm running is. Suppose in my file manager I have /home/test-directory/win open and I'm trying to run /home/test-directory/win/testprogram.exe.

I'm looking for a way to wrap the Wine command in a shell script so I can have it automatically change into /home/test-directory/win before attempting to run wine on testprogram.exe. If I was in /home/other-directory/win, I want it to automatically change into /home/other-directory/win instead before running.

  • Please take the time for the tour and read How to Ask.
    – Murphy
    Commented Sep 30, 2019 at 9:21
  • For programs run with WINE, it's a bit more complicated than just changing to the right directory. You probably also need to set the right WINEPREFIX so that WINE knows what the correct settings, registry file, root directory, drive mappings, etc are for that particular program.
    – cas
    Commented Sep 30, 2019 at 9:42
  • I use WINE as my primary example here because that's just what I happened to be working with lately, but I wanted to make it a topic because I've also had similar issues when running java programs and such. It isn't specifically to solve a problem in WINE so much as it is trying to find a generally convenient way to write startup scripts for programs with this issue. Commented Oct 1, 2019 at 6:23
  • Are you sure your question isn't containing a XY problem?
    – Murphy
    Commented Oct 1, 2019 at 15:16

1 Answer 1


In order to change the working dir of a script to the directory the script is located in, you can use this construct:


cd "$(dirname "$0")"

# Wine command(s) here

This will take the dir of the script you started and change into it before it executes your Wine commands.

  • You're saying I make a shell script like this within whatever directory I'm trying to run the program in right? This is the temporary solution to the problem I implement currently, but I was hoping to find a universal, directory agnostic way to do this with a single script in a bin folder that could spare me writing many startup scripts. Commented Oct 1, 2019 at 6:20
  • At some point you need to know the path of the directory you want to work in. That you want a one-script-to-rule-them-all solution isn't obvious from your question. How do you want to pass the directory you intend to use to the script you want to execute by a click in the file manager? However, using symlinks from the individual working dirs to a central script might work; just try it, but I can't tell if that's a solution to your actual problem as long as you don't provide more details on your requirements. You can just start with this, and improve your solution until you're satisfied.
    – Murphy
    Commented Oct 1, 2019 at 14:58
  • I think you overlooked the key clause of the OP which is: "It isn't treating the file manager's open folder as the current working directory" The important element here is graphical file managers and getting information from them. "How do you want to pass the directory you intend to use to the script you want to execute by a click in the file manager?" That's the point. I want to get information from Nautilus or other graphical file managers about what directory is open when it invokes the script. Commented Oct 1, 2019 at 22:23

You must log in to answer this question.

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