In Unix, How to Run 'Make' in a Directory Without Cd'Ing to That Directory First

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

make -C /path/to/dir

how to set the directory where Makefile exists so that I can run make from different directory using `make -C` option?

If you really use make -C (not make -f) and your Makefile is not included in another, you can simply use the CURDIR variable. GNU make sets it to the absolute path of the current directory when it starts, "after it has processed any -C options". So, in your case it should do exactly what you want.

Else, if you sometimes use make -f or if you have included Makefiles, you can put this as the first line of any of your Makefiles (or, at least, before any include statement):

HERE := $(dir $(lastword $(MAKEFILE_LIST)))

and then use $(HERE) to refer to this Makefile's directory. See the GNU make manual for the details.

Note: I was almost sure this question would be a duplicate. Surprisingly I searched SO for a clear answer and found only old answers that first suggest shell calls before using make built-ins or wrong answers (using firstword instead of lastword, for instance).

How can I run any executable file from outside of current directory?

The correct format would be to either start from root, /, or to use the current directory, .. These examples assume that "~$" is your prompt.

Using / from anywhere on your system

~$ /home/My_folder/Current_folder/a.out

Using . from the /home/My_Folder directory

~$ ./Current_folder/a.out

Specifying path to makefile using make command

All relative paths in the makefile will be relative to your current directory and not the directory of the makefile.

Assuming that you understand that and what you want to do is still going to work then you want the -f flag to specify the makefile to use. (Which is in the man page, the manual and the --help output.)

If, instead, what you mean is you want to cd to somewhere else and run make then perhaps you are looking for (cd /some/path && make)?

make directory structure for each row within the text file in unix

This is what I did:

while read -r line
do
src="${currDir}/${repository_name}-${branch}/$line"
cp -R $src $deploy_loc
done < "$branch.diff"

It reads the branch.diff line by line and copies the files from src to deployloc.

Cygwin configure: error: source directory already configured; run make distclean there first

I can't determine your environment, so I only to guess.

At first, you should execute bootstrap.sh, then execute configure and other make commands.

EDIT:
Direct reason is below code in configure.
You should try delete config.status and execute bootstrap.sh && ./configure.

1715  # test to see if srcdir already configured
1716 if test "`cd $srcdir && pwd`" != "`pwd`" &&
1717 test -f $srcdir/config.status; then
1718 { { echo "$as_me:$LINENO: error: source directory already configured; run \"make distclean\" there first" >&5
1719 echo "$as_me: error: source directory already configured; run \"make distclean\" there first" >&2;}
1720 { (exit 1); exit 1; }; }
1721 fi

Can I point /etc directory somewhere else when I run a specific program

Your custom program might accept some program option (or some environment variable) to override its configuration file. Try to run yourcustomprogram --help to find out (then man yourcustomprogram) and read its documentation.

And you might consider using a bind mount (or a symbolic link) to solve your issue. See also mount(8)

when does executables from bin directory run

for Linux/Unix most executables are stored in /usr/bin or /usr/local/bin/ /usr/sbin or something like that. It is a convention but not a requirement.

you can run them manually ( assuming you have permission to do so )

Linux/Unix has several methods of running files automatically


at boot : files (scripts ) stored in /etc/init.d run at boot
there is also /etc/inittab which can do the same thing


on a schedule: things can be run via cron and each user can
have his or her own cron schedule


on login : each user can set up a ".profile " which is run at login
so you could put stuff in here as well though this is usually
reserved for login setup ( setting your path's and preferences )


if your files in /usr/bin are not references in any of these places then
they should only be running when you the user runs them.

your application should have some doc explaining if it hooks into any the above auto run methods. I know this is a bit vague but the scope of your question is very broad.

Get current directory or folder name (without the full path)

No need for basename, and especially no need for a subshell running pwd (which adds an extra, and expensive, fork operation); the shell can do this internally using parameter expansion:

result=${PWD##*/}          # to assign to a variable
result=${result:-/} # to correct for the case where PWD=/

printf '%s\n' "${PWD##*/}" # to print to stdout
# ...more robust than echo for unusual names
# (consider a directory named -e or -n)

printf '%q\n' "${PWD##*/}" # to print to stdout, quoted for use as shell input
# ...useful to make hidden characters readable.

Note that if you're applying this technique in other circumstances (not PWD, but some other variable holding a directory name), you might need to trim any trailing slashes. The below uses bash's extglob support to work even with multiple trailing slashes:

dirname=/path/to/somewhere//
shopt -s extglob # enable +(...) glob syntax
result=${dirname%%+(/)} # trim however many trailing slashes exist
result=${result##*/} # remove everything before the last / that still remains
result=${result:-/} # correct for dirname=/ case
printf '%s\n' "$result"

Alternatively, without extglob:

dirname="/path/to/somewhere//"
result="${dirname%"${dirname##*[!/]}"}" # extglob-free multi-trailing-/ trim
result="${result##*/}" # remove everything before the last /
result=${result:-/} # correct for dirname=/ case


Related Topics



Leave a reply



Submit