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

4

0

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

Jamie Duncan

Posted 2010-03-09T14:34:02.133

Reputation:

why do you need to cd to the directory to execute your command? Just execute the command on /path/to/pom.xml – None – 2010-03-09T14:42:31.883

This question clearly says you aren't writing any kind of shell script, it belongs on superuser – Tim Post – 2010-03-09T14:52:48.407

1@tim he's trying to avoid programming which should be any programmers first thought ;) – sfussenegger – 2010-03-09T15:06:26.630

@ 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 – None – 2010-03-09T16:56:45.050

Answers

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 \;

sfussenegger

Posted 2010-03-09T14:34:02.133

Reputation: 189

-execdir is exactly what is needed. – William Jackson – 2013-03-14T03:49:11.207

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

Pointy

Posted 2010-03-09T14:34:02.133

Reputation: 763

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.

Brett Porter

Posted 2010-03-09T14:34:02.133

Reputation: 111

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

Marcos Carceles

Posted 2010-03-09T14:34:02.133

Reputation: 111

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.

LenW

Posted 2010-03-09T14:34:02.133

Reputation: 101

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

Cry Havok

Posted 2010-03-09T14:34:02.133

Reputation: 3 486

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.

DolphinDream

Posted 2010-03-09T14:34:02.133

Reputation: 101

This is a reasonable answer –– to a different question.  This has only the slimmest relationship to the question that was actually asked here. – Scott – 2013-03-14T04:13:20.653