There is a file that I would like execute in a different folder, under about four sub-directories.

For example:

My pwd may be /home/directoryA. However, the file I would like execute may be in directoryD. At the moment if I want to execute the file I would need to go to cd /home/directoryA/directoryB/directoryC/directoryD/ and then execute the file. Or I might have do something like src /directoryA/directoryB/directoryC/directoryD/somefile

  1. Is it possible to execute the file without actually being in the directory where the file is?

  2. Is there a shortcut way of executing the file somefile without going into the directory?

  • 1
    Beside the point, but note that directoryD and DirectoryD are not the same. – wjandrea May 17 at 3:33
  • 2
    @wjandrea Plus /home/directoryA is probably meant to be /home/ME/directoryA. Also the cd /directoryA/directoryB.... is missing the ~ prefix. Also the cd ends in .../somefile which is impossible to change directory to a file. There are lots of flaws that need to be overlooked. – WinEunuuchs2Unix May 17 at 4:40
  • I've updated my answer based on new comments read under other answers. – WinEunuuchs2Unix May 17 at 12:32

No you don't need to use:

cd home/directoryA/directoryB/directoryC/DirectoryD

You can simply run the command by prefixing it with its path:


Because you are already in the /home/directoryA you can use the current directory shortcut . and run the command like this:


I noticed OP has expanded scope via comments under other answers. Here is some additional information:

  • To find out where somefile is located use: locate somefile.
  • If somefile was added today you need to first update the locate database by running sudo updatedb.
  • When there are multiple versions of somefile located in the PATH you can find out which one is executed first use which somefile.
  • If you want to run somefile without specifying a directory name in front put it in the path. To check the path use echo $PATH. Common path locations to put somefile are /usr/local/bin (if it uses sudo powers) and /home/your_user_name/bin (you might have to create the directory first).
  • You can also add /home/directoryA/directoryB/directoryC/DirectoryD/ to your path but that would be highly unusual. However you could then simply type somefile no matter what directory you are in and it will run.
  • Of course somefile must be executable which you set with the command: chmod a+x /home/directoryA/directoryB/directoryC/DirectoryD/somefile

Sure! If somefile is marked as executable, you can run it with


Want to know if somefile is executable? Go to its directory and run

find . -maxdepth 1 -perm -111 -type f

to see all the executables in that directory.

  • Ok, can you let me know how I would execute the file if I’m not already in directory A, but in some other directory. – Carltonp May 16 at 23:25
  • Also, can you let me know how to find out what directory a file is located in? – Carltonp May 16 at 23:27
  • 2
    If you want to know if it's executable, why not run ls -l /directoryA/directoryB/directoryC/DirectoryD/somefile ? – wjandrea May 17 at 3:31
  • @Carltonp Please post another question regarding where to find the location of a file. The folks who run this place like the 'one issue per post' idea. - And, my answer works no matter which directory you are in. – K7AAY May 17 at 20:13

The $PATH shell variable contains the directories where the executables are searched. Add the directory containing your executable into $PATH and it can be executed from anywhere.

Add in .bashrc file:

export PATH=$PATH:/../your_directory
  • /.. is the same as /. Is that supposed to be /... ? – wjandrea May 24 at 1:59

There is another way (somehow not already mentioned) by using shell profile (.bashrc, .zshrc etc).

You can run:

# Assuming it is a script you made, changing file permission to make it executable
chmod a+x ~/directoryA/directoryB/directoryC/directoryD/somefile

# Appending your shell profile with an alias to run the script from wherever you are
echo "alias somename=\"source ~/directoryA/directoryB/directoryC/directoryD/somefile\"" >> ~/.profile

# replace ~/.profile with config file of whichever shell you use
# Also replace source with python if the script is a python script or whichever interpreter it requires for execution
# Make sure you have #!/usr/bin/env python or #!/path/to/interpreter on your computer as the first line of your script

Although above method allows the script to be run from anywhere you should make sure the script doesn't depend on the pwd (present working directory) for execution (unless intended).

You can then run the script as executable in any directory, like:


P.S.: As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use.

  • @peter: Only the respective question author can accept an answer. If you're the author of this question posting from a different account please log into the original account to accept an answer. If you don't have access to your original account any more you can use the contact form to petition to have your accounts merged. – David Foerster May 17 at 22:15
  • -1 Why source the file instead of just run it? (Though if you do need to source it, it doesn't need to be executable.) Also it's better practice to define aliases in .bashrc, not .profile. And why create an alias instead of adding the directory to the PATH? – wjandrea May 24 at 2:18
  • Oh, I think there may have been confusion around OP's use of src in the question. I don't think that's supposed to mean the same thing as source. – wjandrea May 24 at 2:24
  • i did say ask to replace source with name of interpreter like sh, python, etc, but gave source in example due to OP writing src which seemed like a misspelled source to me also doesn't refer to any executables. As for why not append the directory to PATH was simply under the assumption that only the single executable was to be added and not a directory full of executables like adb-platform-tools in which case appending the directory path to PATH would be the method to use. – ToxicMender May 24 at 21:07

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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