Code:
#!/bin/bash
dir=.
exts=( avi mkv mp4 mov ) # array of relevant extensions
# associative array of counters, count[X] = number_of_X
declare -A count
# construct the regex matching desired file types
printf -v rgx "%s|" "${exts[@]}"
rgx=".*[.](${rgx%?})"
# loop through the output of find supplied with the prepared regex
while read -rd $'\0' f
do
ext=${f##*.} # get file extension
ext=${ext,,} # normalize ext to lowercase
count[$ext]=$(( ${count[$ext]}+1 )) # add one to the counter
done < <( find "$dir" -regextype posix-extended -iregex "$rgx" -type f -print0 )
# print the key/value pairs present in the count array
for k in ${!count[@]}; do echo "$k: ${count[$k]}"; done
Code:
$ cd test
$ ls
test2.avi test2.mp4 test.mkv test.mp4
test2.MKV test.AVI test.Mov videos.sh
$ ./videos.sh
mkv: 2
mp4: 2
mov: 1
avi: 2
redirecting to file inside the script is not necessary. You can redirect the script itself wherever you want, which gives you way more flexibility
Code:
./script
./script | some | commands
./script > file
Bookmarks