The obvious solution would be to change data.sh
and make it accept one or more arguments, those being the files (and perhaps directories) being worked on.
Say the script currently contains:
#!/bin/bash
some-command ./* # ← this is where it works on all files
Then you'd change it to:
#!/bin/bash
some-command "$@"
Here, $@
is all arguments passed to data.sh
, and you'd only need to call it like this:
/path/to/data.sh /path/to/another/directory/*
Then the script would run on all files in the other directory. Of course, without knowing the exact inner workings of data.sh
it's impossible to suggest how to change it, specifically.
If you want to keep data.sh
as-is, you need to change the current directory every time you call it. This will make your script "think" it's inside another directory.
You could achieve this by using cd
(or pushd
/popd
) from a shell launched by find
:
find . -name "data.sh" -exec sh -c 'cd "$(dirname "$0")" && ./data.sh' {} \;
Or you could temporarily cd
the directory in a subshell (this is the parentheses around cd
), maybe with globs so you can skip using find
(the **
will recursively match all directories):
shopt -s globstar
for script in **/data.sh; do
( cd "$(dirname "$script")" && ./data.sh )
done
If you hadn't used a subshell in the above command, the second cd
call would fail since it'd try to cd
from the first found directory that contains data.sh
. You can avoid this with pushd
/popd
:
for script in **/data.sh; do
pushd "$(dirname "$script")" && ./data.sh
popd
done