0

I am trying to delete all *.pyc and pycache, and any other silly files languages need to run that i don't want to see. The closest I've gotten is

sudo rm -rf **/*__pycache__

answer, which doesn't work deep down the path, and

sudo rm -R -f {__pycache__,.*.pyc}

which didn't work for pycache folder.

webapi/__pycache__
webapi/cool_app/__pycache__
webapi/cool_app/bad_file.pyc
webapi/cool_app/keep_this_awesomeness.py
webapi/cool_app/sweet_folder/__pycache__
webapi/cool_app/sweet_folder/bad_file.pyc
webapi/cool_app/sweet_folder/keep_this_awesomeness.py

Only

webapi/cool_app/keep_this_awesomeness.py 
webapi/cool_app/sweet_folder/keep_this_awesomeness.py

remain.

3
find . \( -name __pycache__ -o -name "*.pyc" \) -delete
8
  • 2
    I think you need parenthesis around that: find . \( -name __pycache__ -o -name *.pyc \) -delete
    – ilkkachu
    Aug 25 '16 at 20:09
  • wow you guys are sick. <3
    – codyc4321
    Aug 25 '16 at 20:14
  • why you need the parens around?
    – codyc4321
    Aug 25 '16 at 20:14
  • 1
    @codyc4321 because touch a b; find . -name a -o -name b -delete; ls a b
    – thrig
    Aug 25 '16 at 20:29
  • ok, updated.... Aug 25 '16 at 20:36
3

If your shell supports, you can do the recursive traversal with the shell itself and use rm to remove the files.

For example, in bash, globstar shell option lets the glob pattern ** to match any depth of subdirectory and also you can use regular globbing operators to match files.

In your case:

rm **/{*.pyc,__pycache__}

would do.

At first, enable globstar:

shopt -s globstar
0
0

In you case, you could also do

find . -name "__pycache__" -o -name "*.pyc" | xargs rm -rf

Made only with built-in commands. It avoids you to enable globstar.

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.