Search code examples
linuxfindrm

How to remove all subfolders that contain certain file and do not contain other file


I want to remove all direct subfolders of the working directory that contain the file core.properties.unloaded and do not contain the file core.properties. I know this must be a combination of find and rm. But how to combine?

Thanks for your help!


Solution

  • Starting like so

    $ find . -name "*"
    .
    ./dir1
    ./dir1/core.properties.unloaded
    ./dir2
    ./dir2/core.properties
    ./dir3
    ./dir3/core.properties
    ./dir3/core.properties.unloaded
    

    I ran this

    for i in `find -mindepth 2 -maxdepth 2 -name "core.properties.unloaded" | awk -F'/' '{print $2}'`
    do
      if [ ! -f "$i"/core.properties ]; then 
        rm -rf "$i"
      fi
    done
    

    And then the find command demonstrates that only dir1 was removed (the other two directories contain core.properties or do not contain core.properties.unloaded) -

    $ find . -name "*"
    

    . ./dir2 ./dir2/core.properties ./dir3 ./dir3/core.properties ./dir3/core.properties.unloaded