Perfectly Random

machine learning and stuff

Search & destroy files

To recursively seek and destroy all .pyc files within current directory. The following command does that for us in bash shell.

find . -name "*.pyc" -type f -exec rm "{}" \;

Simply typing find . -name "*.pyc" -type f would list all files (and not directories) whose name matches the glob *.pyc within the directory . (current directory). find also accepts -exec option in both Linux and MacOS (though -delete option may not always be available). See other variants here. The above command works even for filenames or paths that have spaces in them. In contrast, using find . -name "*.pyc" -type f | xargs rm is risky with spaces. Here is a demo:

$ find . -name "*.pyc" -type f
./hello world.pyc
./p q r/this has spaces.pyc
./p q r/d.pyc

$ find . -name "*.pyc" -type f -exec rm "{}" \;

$ find . -name "*.pyc" -type f
# Nothing anymore!