Find all subdirectories of the current one that contain nothing but a pycache directory
- User
Find all subdirectories of the current one that contain nothing but a
__pycache__directory Assistant
You can find all subdirectories that contain only a
__pycache__directory using this command:find . -type d -name "__pycache__" -exec sh -c ' parent=$(dirname "$1") if [ "$parent" != "." ] && [ $(ls -A "$parent" | wc -l) -eq 1 ]; then echo "$parent" fi ' _ {} \;Alternatively, here's a more readable approach using a small shell script:
find . -type d ! -name "__pycache__" -exec sh -c ' dir="$1" contents=$(ls -A "$dir" 2>/dev/null) if [ "$contents" = "__pycache__" ]; then echo "$dir" fi ' _ {} \;Or if you prefer a Python solution:
import os for root, dirs, files in os.walk('.'): # Skip the current directory itself if root == '.': continue # Check if directory contains only __pycache__ and no files if dirs == ['__pycache__'] and not files: print(root)All of these will recursively search from the current directory and output paths to directories that contain nothing but a
__pycache__subdirectory.