I looking for a command or bash script to delete all folders except if they have a specific file type (*.pdf) in the first level subfolder.
folder01
a.txt
y.txt
folder02
b.pdf
z.txt
folder03
h.txt
folder03.1
c.pdf
In the example above folder01 and folder03 needs to be deleted.
My attempt:
#!/bin/bash
shopt -s globstar
Loop through every subdirectory.
for d in */; do
f=("$d"/)
if [[ -f "$f" && ! "${f##/}" =~ ^.pdf$ ]]; then
# echo to ensure a test run; remove when verified.
echo rm -r -- "$d"
fi
done