Ubuntu – How to delete all files in particular folder present in current directory sub-folders excluding a certain file

command line

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:

find . -path '*/migrations/__init__.py' -exec truncate -s 0 {} + -o -path '*/migrations/*' -delete

This runs find in the current directory (.), and:

  • for anything matching migrations/__init__.py, it will run the truncate command. truncate -s 0 <file> reduces the file to size 0 (empty);
  • failing that match, for anything matching migrations/*, it will delete it;
  • the * in -path matches / unlike the * in bash.

Example:

$ tree                                                                                                                                   
.
├── 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

9 directories, 15 files
$ find . -path '*/migrations/__init__.py' -exec truncate -s 0 {} + -o -path '*/migrations/*' -delete                
$ tree                                                                                              
.
├── folder_1
│   └── migrations
│       └── __init__.py
├── folder_2
│   └── migrations
│       └── __init__.py
└── folder_3
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    └── migrations
        └── __init__.py

6 directories, 6 files

$ wc -l */migrations/__init__.py
0 folder_1/migrations/__init__.py
0 folder_2/migrations/__init__.py
0 folder_3/migrations/__init__.py
0 total