Suppress messages in make clean (Makefile silent remove)

Solution 1:

To start with: the actual command must be on the next line (or at least that is the case with GNU Make, it might be different with other Make's - I'm not sure of that)

clean:
    rm -rf *.o

(note, you need a TAB before rm -rf *.o as in every rule)

Making it silent can be done by prefixing a @:

so your makefile becomes

clean:
    @rm -rf *.o

If there are no *.o files to delete, you might still end up with an error message. To suppress these, add the following

clean:
    -@rm -rf *.o 2>/dev/null || true
  • 2>/dev/null pipes any error message to /dev/null - so you won't see any errors
  • the - in front of the command makes sure that make ignores a non-zero return code

Solution 2:

In fact I was looking for something else, adding this line to the Makefile :

.SILENT:clean

while execute every step of the "clean" target silently.

Until someone point some drawback to this, I use this as my favourite solution!

Solution 3:

I'm responding to this ancient topic because it comes up high in search and the answers are confusing. To do just what the user wants,all that is needed is:

clean:
    @rm -f *.o

The @ means that make will not echo that command. The -f argument to rm tells rm to ignore any errors, like there being no *.o files, and to return success always.

I removed the -r from the OPs example, because it means recursive and here we are just rming .o files, nothing to recurse.

There's no need for the 2>&1 >/dev/null because with the -f there will be no errors printed.

.SILENT: clean

works in place of the @, but it isn't at the same place in the Makefile as the command that it affects, so someone maintaining the project later might be confused. That's why @ is preferred. It is better locality of reference.