37

may be it seems strange for you, but I want to run command in a specific folder without changing the current folder in the shell. Example - this is what I usually do:

~$ cd .folder
~/.folder$ command --key
~/.folder$ cd ..
~$ another_command --key

Though I want something like this:

~$ .folder command --key
~$ another_command --key

Is it possible?

2
  • Can't you do ~/.folder/command --key ? Does the command require your current directory to be ~/.folder ? Jan 22, 2014 at 16:58
  • @glennjackman Commands such as git pull require to change folder, so the question is useful. )
    – Dr_Zaszuś
    Jul 11, 2020 at 14:59

4 Answers 4

76

If you want to avoid the second cd you can use

(cd .folder && command --key)
another_command --key
4
  • Very quick answer! I even can't to accept it because system doesn't allow me)) Jan 22, 2014 at 16:12
  • 2
    magic parenthesis! how does that work? +1
    – rusty
    Jan 22, 2014 at 18:06
  • The commands within the parenthesis are run in a new shell process so changing the directory, setting environment variables etc. inside the parenthesis do not affect the parent shell that runs the other commands. Jan 22, 2014 at 18:25
  • So sad that bash can't do this with pre-setting env variable like this: PWD=/some/path command. This seems more organic while sub-shell looks like hack
    – oxfn
    Apr 21, 2019 at 18:56
12

Without cd... Not even once. I found two ways:

# Save where you are and cd to other dir
pushd .folder
command --key
# Get back where you were at the beginning.
popd
another_command --key

and second:

find . -maxdepth 1 -type d -name ".folder" -execdir command --key \;
another_command --key
1

I had a need to do this in a bash-free way, and was surprised there's no utility (similar to env(1) or sudo(1) which runs a command in a modified working directory. So, I wrote a simple C program that does it:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

char ENV_PATH[8192] = "PWD=";

int main(int argc, char** argv) {
    if(argc < 3) {
        fprintf(stderr, "Usage: in <dir> <cmd> [<args>...]\n");
        return 1;
    }

    if(chdir(argv[1])) {
        fprintf(stderr, "Error setting working directory to \"%s\"\n", argv[1]);
        return 2;
    }

    if(!getcwd(ENV_PATH + 4, 8192-4)) {
        fprintf(stderr, "Error getting the full path to the working directory \"%s\"\n", argv[1]);
        return 3;
    }

    if(putenv(ENV_PATH)) {
        fprintf(stderr, "Error setting the environment variable \"%s\"\n", ENV_PATH);
        return 4;
    }

    execvp(argv[2], argv+2);
}

The usage is like this:

$ in /path/to/directory command --key
1

A simple bash function for running a command in specific directory:

# Run a command in specific directory
run_within_dir() {
    target_dir="$1"
    previous_dir=$(pwd)
    shift
    cd $target_dir && "$@"
    cd $previous_dir
}

Usage:

$ cd ~
$ run_within_dir /tmp ls -l  # change into `/tmp` dir before running `ls -al`
$ pwd  # still at home dir
1
  • You don't need to remember the previous dir if you run the function in a subshell: run_within_dir() ( ... ) using parentheses instead of braces. Jul 11, 2020 at 17:50

You must log in to answer this question.

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