UNIX find dirs that contain a particular file (pom.xml) then execute command in that dir

Solution 1:

find has -exec and -execdir. For a maven build, you'd do

# executes `mvn clean install` in any directory where pom.xml was found
find . -name pom.xml -execdir mvn clean install \;

Solution 2:

This will find the directories:

find . -name pom.xml -printf '%h\n'

then you could read the directories and run your command:

find . -name pom.xml -printf '%h\n' | while read dir; do ( cd "$dir"; command ... ) done

If you're paranoid about embedded newlines in directory names (and seriously, what's wrong with people?) you can use "\0" in the printf, and then use xargs to run the command:

find . -name pom.xml -printf '%h\0' | xargs -0 -L 1 sh -c 'dir="$0"; cd "$dir"; command ...'

Solution 3:

there's also:

find . -type f -exec mvn -f {} clean install \;

However, I think you should be looking at the available Maven options to do this. While it's no longer present in Maven 3.x, Maven 2.x has the -r option which does exactly this. A better way is to create an aggregating pom.xml that contains the desired files as modules, and you can then use the -rf and -pl to selectively choose which to build.

Solution 4:

If what you want is to apply the svnignore properties based on a file from which you don't know the absolute path you could use:

#!/bin/sh
find -name pom.xml -execdir pwd \; | while read project_dir
do
svn propset svn:ignore -F support-tools/svn/svnignore.cfg $project_dir
done

I use this one in my current projects, as each developer has the svn checkout on a different location and it doesn't change the working dir from which to execute the svn command. For other purposes use

find -name -execdir