Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I wan't to delete all *.o files in a directory and its sub-directories. However I get an error:

sashoalm@aspire:~/.Workspace.OLD$ rm -r *.o
rm: cannot remove `*.o': No such file or directory

On the other hand, rm *.o works, but it's not recursive.

share|improve this question

marked as duplicate by slm, terdon, Braiam, Anthon, Zelda Feb 22 '14 at 14:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 61 down vote accepted

That is evil: rm -r is not for deleting files but for deleting directories. Luckily there are probably no directories matching *.o.

What you want is possible with zsh but not with sh or bash (new versions of bash cannot do this by default but if the shell option globstar is enabled: shopt -s globstar). The globbing pattern is **/*.o but that would not be limited to files, too (maybe zsh has tricks for the exclusion of non-files, too).

But this is rather for find:

find . -type f -name '*.o' -delete

or (as I am not sure whether -delete is POSIX)

find . -type f -name '*.o' -exec rm {} +
share|improve this answer

That's not quite how the -r switch of rm works:

   -r, -R, --recursive
          remove directories and their contents recursively

rm has no file searching functionality, its -r switch does not make it descend into local directories and identify files matching the pattern you give it. Instead, the pattern (*.o) is expanded by the shell and rm will descend into and remove any directories whose name matches that pattern. If you had a directory whose name ended in .o, then the command you tried would have deleted it, but it won't find .o files in subdirectories.

What you need to do is either use find:

find . -name '*.o' -delete

or, for non-GNU find:

find . -name '*.o' -exec rm -r {} \;

Alternatively, if you are using bash you can enable globstar:

shopt -s globstar
rm -r -- **/*.o

NOTE: all three options will delete directories whose name ends in .o as well, if that's not what you want, use one of these:

find . -type f -name '*.o' -delete
find . -type f -name '*.o' -exec rm {} \;
rm -- **/*.o
share|improve this answer

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