I'm trying to check a bunch of folders on my NAS to see if they contain a certain file type, (movies of any kind, *mpg, avi, mkv and so on). My script/oneliner looks like this,
"find . -maxdepth 1 -mindepth 1 -type d | while read dir; do [[ ! -f "$dir/*.avi" -o -f "$dir/*.mpg" -o -f "$dir/*.mkv" -o -f "$dir/*.iso" ]] && echo "$dir has no moviefile"; done"
but it doesn't seem to work properly, it catches folders that DO contain the filetypes I'm trying to screen for. And I can't check for empty folders, because some contain jpgs and .nfo files. There are some 150+ folders with subfolders in almost each.
I am using some version of busybox (BusyBox v1.16.1 (2014-12-13 05:30:51 CST)) and it's a bit crippled.
So, I wan't to check folders (1 or 2 deep) that does NOT have a movie-file in it. The subfolders are usually named extrafanart and extrathumbs, but some have more movies in them, but disregard that, I'm going to organize it in a simpler structure.
I'm going to pose a completely different solution: Compare the list of folders with the list of folders that have your target files:
diff <(ls -d */) <(ls -d */* | sed -n '/\.\(avi\|mpg\|mkv\|iso\)/s|/[^/]*$|/|p' | sort | uniq)
the ls -d
commands can be replaced by equivalent find commands if you're willing to deal with a longer command in order to not potentially run into a command line length limit.
This method has the advantage that if you are searching for a large number of file types, you can just add more to the list -- it doesn't individually search for each type.
The somewhat monstrous sed
command