I want to, from a root dir, find all dirs that contain a certain file name (pom.xml), then cd to that dir and execute a command. I can do this in a script but I was hoping to do this with a single line command.

so far I have

find . -type f -name 'pom.xml' |sed 's#\(.*\)/.*#\1#'

This gives the dirs I need... now I need to cd into each and execute my command

link|improve this question
why do you need to cd to the directory to execute your command? Just execute the command on /path/to/pom.xml – dnagirl Mar 9 '10 at 14:42
This question clearly says you aren't writing any kind of shell script, it belongs on superuser – Tim Post Mar 9 '10 at 14:52
1  
@tim he's trying to avoid programming which should be any programmers first thought ;) – sfussenegger Mar 9 '10 at 15:06
@ dnagirl the command is executing a script that creates new dirs etc in the dir with the pom.xml file @ want is as a one liner so its easy to distribute – Jamie Duncan Mar 9 '10 at 16:56
I might have put this in SUper User in the first place, but it is programmy enough to stay here as far as I'm concerned. – dmckee Mar 9 '10 at 22:14
feedback

migrated from stackoverflow.com Mar 11 '10 at 14:24

This question came from our site for professional and enthusiast programmers.

6 Answers

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 ...'
link|improve this answer
feedback

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 \;
link|improve this answer
feedback

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.

link|improve this answer
feedback

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
link|improve this answer
feedback

You could use xargs :

>

 xargs -- construct argument list(s) and execute utility

SYNOPSIS

  xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
       [-L number] [-n number [-x]] [-P maxprocs] [-s size]
       [utility [argument ...]]

DESCRIPTION

 The xargs utility reads space, tab, newline and end-of-file delimited
 strings from the standard input and executes utility with the strings

as arguments.

 Any arguments specified on the command line are given to utility upon
 each invocation, followed by some number of the arguments read from the
 standard input of xargs.  The utility is repeatedly executed until stan-
 dard input is exhausted.
link|improve this answer
feedback

How about:

find / -type f -name 'pom.xml' -print | while read FILE; do cd `dirname ${FILE}` && /run/my/script; done

If you can change the script so that it takes a directory as an argument then the following should work:

find / -type f -name 'pom.xml' -exec /run/my/script `dirname {}` \;
link|improve this answer
feedback

Your Answer

 
or
required, but never shown