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?

  • 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 8, 2016 at 0:30
  • The above is a more specific case of 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 8, 2016 at 1:33

1 Answer 1


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 {} +

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.