Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to run an executable file from a different directory without cd or modifying the PATH?

Writer Andrew Mclaughlin

Variations similar to what I want to ask exist but I could not find the exact question, so I'll ask it here.

Let's say I have a comiled .c file resulting in the executable ex which is located as /mnt/f/C_F/ex. I can very well execute it from within /mnt/f/C_F by ./ex but what if I want to do that from /mnt/f say (or any directory for that matter). I don't want to add /mnt/f/C_F/ to PATH for this.

I could not find a solution and I suppose it's not possible to do so. Please confirm this.

3 Answers

You can always use the relative or full path to your executable

When you're in /mnt/f, you can run:

C_F/ex
# or
/mnt/f/C_F/ex
5

Use an absolute path to that file.

/mnt/f/C_F/ex

This should always work regardless of your current working directory. Generally, you probably should prefer absolute paths within scripts for this reason (though there are important exceptions).

Any path1 to the executable file that contains at least one / works. The presence of a / character in a command name tells the shell that you are asking it to run a specific file and that you are specifying the specific place where that file is to be found. This is part of the syntax the shell expects; in most other contexts it is acceptable for (relative) paths to contain no / character.

For an executable that is not in the current directory, that's any path to it at all. There is no way to write the path of a file in another directory without using at least one /. This makes running files in other directories the simpler case, even though it is less familiar.

  • Absolute paths--like /mnt/f/C_F/ex--start with /.

  • Relative paths to a file in another directory--C_F/ex when you are in the /mnt/f directory--always contain a /, even though it is not at the beginning. A relative path without a / is just a filename, identifying a file in the current directory. (The converse is not true though. A relative path may contain a / yet still refer to a file in the current directory; see below.)

    Note that relative paths must still actually refer to the file you want if they are to work, and whether or not a relative path refers to the file you want depends on what directory you are currently in, since the are resolved relative to the current directory.

For an executable that is in the current directory, the shortest relative path does not generally work, because it does not contain a /. Thus, this is the more complicated case, even though it is more familiar. When you are in the directory where an executable ex is located, you can't just run ex, even though ex is a perfectly good relative path to that executable.

The reason it does not work is that a command name that does not contain a / is not treated as a relative path or any path. Instead, it is treated as a filename and searched for in the directories listed in $PATH.2

This is why you use ./ex when you're in the same directory as ex. Every directory contains a . entry that refers to the directory itself. This provides a way to write a relative path that identifies the same file as ex but has a / in it and is thus usable as a command name that the shell treats as a path.

A ./ prefix is thus not a special syntax, but simply the second-simplest way to write a relative path to a file in the current directory, and the simplest way to do so of those that contain a /.


1 In this answer, I use "path" to mean "pathname." This is the most common use of "path," but not the only one. "Path," as I use the term here, should not be confused with the executable search path, which is the collection of directories whose paths are listed, separated by : characters, in the $PATH environment variable.

2 Command names that do not contain a / may also be resolved as non-external commands, such as shell builtins and shell functions.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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