4

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

4
  • 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, 2010 at 14:42
  • This question clearly says you aren't writing any kind of shell script, it belongs on superuser
    – Tim Post
    Mar 9, 2010 at 14:52
  • 1
    @tim he's trying to avoid programming which should be any programmers first thought ;) Mar 9, 2010 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, 2010 at 16:56

7 Answers 7

6

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 \;
1
  • -execdir is exactly what is needed. Mar 14, 2013 at 3:49
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 ...'
1

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.

1

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
0

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

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.

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

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.

You must log in to answer this question.