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

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

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

  • 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

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 \;
  • -execdir is exactly what is needed. – William Jackson Mar 14 '13 at 3:49

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 ...'

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.


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:

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

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

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 {}` \;

For finding the unique folders containing a given file type you can use:

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

This will list all the unique directories including PNG files.

  • This is a reasonable answer –– to a different question. This has only the slimmest relationship to the question that was actually asked here. – Scott Mar 14 '13 at 4:13

You could use xargs:

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


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


 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy