0

Currently, I have a command that loops over a list of directories(which I don't know the names of) and then cd into directory to finally execute a maven install command. However, I noticed that only one directory holds the pom.xml. It is unnecessary to run the maven command on those other directories. Is there a way to loop through directories and stop in the one that holds the pom.xml, then execute the maven command and break out of the loop?

for d in */ ; do (cd "$d" && maven install -Dmaven.test.skip=true); done
MaryCoding
  • 624
  • 1
  • 9
  • 31
  • Possible duplicate of [How can I recursively find all files in current and subfolders based on wildcard matching?](https://stackoverflow.com/q/5905054/608639), [Recursively find all files that match a certain pattern](https://stackoverflow.com/q/23207896/608639), [Find file in directory from command line](https://stackoverflow.com/q/656741/608639), etc. – jww Oct 05 '17 at 15:30

1 Answers1

3

You could replace your loop with the find command. For example:

find * -name pom.xml -execdir maven install -Dmaven.test.skip=True \;

See the man page for details. The -execdir option runs the given command(s) inside the directory containing pom.xm. This will recurse through your entire directory tree; to make it more exactly equivalent to your shell script, you can limit the depth to which it will descend:

find * -name pom.xml -maxdepth 1 -execdir maven install -Dmaven.test.skip=True \;
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Any idea why i get this `find: The relative path $Path is included in the PATH environment variable, which is insecure in combination with the -execdir action of find. Please remove that entry from $PATH` ? I am running the command through a jenkins groovy script inside a `workspace` – MaryCoding Oct 05 '17 at 15:41
  • [here](https://askubuntu.com/questions/621132/why-using-the-execdir-action-is-insecure-for-directory-which-is-in-the-path) is an explanation about why `-execdir` is a problem if you are iterating through directories that are included in your `$PATH`. – larsks Oct 05 '17 at 17:31