Recursively deleting all "*.foo" files with corresponding "*.bar" files

bash, recursion, unix, zsh

Solution

Remember to first take a backup before you try this `find` and `rm` command.

Use this `find`:

find . -name "*.foo" -execdir bash -c '[[ -f "${1%.*}.bar" ]] && rm "$1"' - '{}' \;

Problem

How can I recursively delete all files ending in `.foo` which have a sibling file of the same name but ending in `.bar`? For example, consider the following directory tree: ``` . ├── dir │   ├── dir │   │   ├── file4.bar │   │   ├── file4.foo │   │   └── file5.foo │   ├── file2.foo │   ├── file3.bar │   └── file3.foo ├── file1.bar └── file1.foo ``` In this example `file.foo`, `file3.foo`, and `file4.foo` would be deleted since there are sibling `file{1,3,4}.bar` files. `file{2,5}.foo` should be left alone leaving this result: ``` . ├── dir │   ├── dir │   │   ├── file4.bar │   │   └── file5.foo │   ├── file2.foo │   ├── file3.bar └── file1.bar ```

Original source