2

I have a command in my makefile for clean:

rm -rf */*.o */*.cmo */*.cmi */*.cmx */*.cma */*.cmxa */*.annot

Now I would like to write a cleanpartial that removes these files except those in the folder frontend/ and the folder frontend/gen/.

Does anyone know how to write this command properly?

SoftTimur
  • 687
  • Do you mean that matching files directly within frontend should be left alone, but matching files in e.g. frontend/somedir should be deleted? What about matching files in frontend/gen/somedir? (The answer I gave simply skips everything within frontend and all of its subdirectories, that being the simplest interpretation.) – Wildcard Apr 08 '16 at 00:30
  • The above is a more specific case of http://unix.stackexchange.com/q/87258/135943 as it involves pattern matching as well as excluding a directory; not quite a duplicate but closely related. – Wildcard Apr 08 '16 at 01:33

1 Answers1

3

You should use find for this.

You can safely test with the following command:

find . \( -name frontend -prune \) -o -type f \( -name \*.o -o -name '*.cm[oixa]' -o -name \*.cmxa -o -name \*.annot \) -print

Once you're happy with the list of files that gets printed, run the real command to delete the lot of them:

find . \( -name frontend -prune \) -o -type f \( -name \*.o -o -name '*.cm[oixa]' -o -name \*.cmxa -o -name \*.annot \) -exec rm -f {} +
Wildcard
  • 36,499