There are lots of posts where people asking how to delete files except ones, but my question is more specific. There are several folders named migrations
in current directory's sub-folders, which contain __init__.py
file and some more. How to delete all files except __init__.py
in all sub-folders named migrations
from current folder?
UPD: And is it possible to overwrite __init__.py
with empty file?
Folder structure:
├── folder_1
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-35.pyc
│ │ └── __init__.cpython-35.pyc
├── folder_2
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-35.pyc
│ │ └── __init__.cpython-35.pyc
├── folder_3
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-35.pyc
│ │ └── __init__.cpython-35.pyc
Best Answer
With
find
:This runs
find
in the current directory (.
), and:migrations/__init__.py
, it will run thetruncate
command.truncate -s 0 <file>
reduces the file to size 0 (empty);migrations/*
, it will delete it;*
in-path
matches/
unlike the*
in bash.Example: