Alan Dipert

Sunday May 25, 2008
Category  

I’ve got a directory on my computer with lots of toy projects, many of which have Makefiles and build.xml files.

Every now and then I back up this directory, and to save space and time I decided I should probably figure out a way to ‘make clean’ on all of them before making a big tarball of source, object files, and executables.

I didn’t feel like handcoding a “meta Makefile” with a “make clean” directive for each subdirectory, so I pooped out cleanall.sh:

#!/bin/bash
# cleanall.sh - executes 'make clean' in subdirectories with Makefiles
MYDIR=`pwd`
for DIR in `find . -name Makefile -maxdepth 2 -exec dirname {} ';'`; do
	echo Cleaning $DIR...
	cd $DIR; make clean
	cd $MYDIR
done
MYDIR=`pwd`

save the current directory in MYDIR.

for DIR in `find . -name Makefile -maxdepth 2 
-exec dirname {} ';'`; do

execute find(1) starting in the current directory looking for files named Makefile and going no more than 2 directories deep. Execute dirname(1) on what’s found, and store the result in DIR.

cd $DIR; make clean

move into the Makefile’s directory and execute ‘make clean.’

cd $MYDIR

go back to the starting directory

This tiny script is a good example of how awesome find -exec is, and is a good starting point for doing lots of different things on files you dig up using find(1).

Comment

  1. But what if there are spaces or quotes or something in the paths that find returns? That'll mess up the loop.

    How about:

    find . -name Makefile -maxdepth 2 \ -execdir make clean ';'

    your mom · May 28, 02:49 PM · #

  2. G4aPcP udawxrowtgsq, [url=http://hcdbufudzmtl.com/]hcdbufudzmtl[/url], [link=http://nktwzznabqwx.com/]nktwzznabqwx[/link], http://hjlssbvoqvhx.com/

    ikbquqexd · Jan 13, 01:39 AM · #

  3. YpOtYN

    NkPAfU · Mar 9, 09:18 AM · #