4

Why does command:

rm **/*.pyc

removes nothing?

What is the proper way to achieve expected behavior?

oguz ismail
  • 1
  • 16
  • 47
  • 69
kharandziuk
  • 12,020
  • 17
  • 63
  • 121

1 Answers1

9

To make your command work, you need to enable globstar first:

shopt -s globstar

When globstar mode is enabled, **/*.pyc will expand to match files ending in .pyc in the current directory and all subdirectories.

Alternatively you could just use find:

find -name "*.pyc" -delete

This will search for anything ending in .pyc in the current directory and all subdirectories, deleting whatever it finds. To restrict it to only match files, you can add the -type f switch as well, although this may not be a problem (and wouldn't have been the case in your original command).

Or, if your version of find doesn't understand -delete:

find -type f -name "*.pyc" -exec rm {} \;
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • this is an incredible answer - Been devving for 10+ years and never come across this! works so easily - even on Windows Git Bash - Why is this not on by default! – danday74 Sep 22 '21 at 19:21