views:

525

answers:

3

In Unix, can I run 'make' in a directory without cd'ing to that directory first?

+10  A: 

make -C /path/to/dir

bmotmans
Grump - that isn't in standard make; it must be a GNU extension. Since you say Linux and Unix, it isn't clear which you want, but the -C option won't work on Solaris 10 (/usr/ccs/bin/make), AIX (/usr/bin/make), or HP-UX 11.23 (/usr/bin/make). Still, 1 out of 4 isn't too bad.
Jonathan Leffler
+14  A: 

As noted in other answers, make(1) has a -C option for this; several commands have similar options (e.g. tar). It is useful to note that for other commands which lack such options the following can be used:

(cd /dir/path && command-to-run)

This runs the command in a sub-shell which first has its working directory changed (while leaving the working directory of the parent shell alone). Here && is used instead of ; to catch error cases where the directory can not be changed.

Dave C
Very good answer giving detail to a simple question!
ypnos
+1  A: 

If the reason you don't want to cd to a directory is because you need to stay in the current directory for a later task, you can use pushd and popd:

pushd ProjectDir ; make ; popd

That goes into the ProjectDir, runs make, and goes back to where you were.

EnigmaCurry