24

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?

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

If you want to avoid the second cd you can use

(cd .folder && command --key)
another_command --key
| improve this answer | |
  • Very quick answer! I even can't to accept it because system doesn't allow me)) – Timur Fayzrakhmanov Jan 22 '14 at 16:12
  • 1
    magic parenthesis! how does that work? +1 – precise Jan 22 '14 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. – Florian Diesch Jan 22 '14 at 18:25
  • 10
    I'd change the ; to && for good measure. If the cd fails (e.g. because you typoed the directory name), you probably don't want to run the command. – geirha Jan 24 '14 at 9:35
  • +1 for @geirha's comment. It's a really important point. OP, would you consider editing? – jaybee Jul 10 '17 at 4:11
8

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
| improve this answer | |
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
| improve this answer | |
  • 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. – glenn jackman yesterday
0

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
| improve this answer | |

Your Answer

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

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