Home Looping over all files of certain extension in a directory
 I wrote a small script that unzips all the *.zip files in the current directory to extract only *.srt files directory to a newly created directory. It then loops over all the *.mkv files in the current directory to get their name and then changes subs/*.srt file name to produce a new file name that is exactly as *.mkv file name. The script works when there is one zip file and one mkv file but when there are more files it produces bad filenames. I cannot track why this is the case. Now I figured out when this is the case. EDIT I managed to narrow down the scenarios when file names are changed in erroneous way. Let's say in a current directory we have three *.mkv files: (sorted alphabetically) $ls -1a *.mkv Home.S06E10.1080p.BluRay.x264-PRINTER.mkv Home.S06E11.1080p.BluRay.x264-PRINTER.mkv Home.S06E12.1080p.BluRay.x264-PRINTER.mkv  and three *.srt files: $ ls -1a *.srt Home.S06E10.srt Home.S06E11.BDRip.X264-PRINTER.srt Home.S06E12.BDRip.X264-PRINTER.srt  When I run the script, I get: subs/Home.S06E10.srt -> subs/Home.S06E10.1080p.BluRay.x264-PRINTER.srt subs/Home.S06E10.1080p.BluRay.x264-PRINTER.srt -> subs/Home.S06E11.1080p.BluRay.x264-PRINTER.srt subs/Home.S06E11.1080p.BluRay.x264-PRINTER.srt -> subs/Home.S06E12.1080p.BluRay.x264-PRINTER.srt  As you see, Home.S06E10.srt is used twice #!/usr/bin/env bash mkdir -p subs mkdir -p mkv-out mkdir -p subs-bak # unzip files, maybe there are subtitles in it... for zip in *.zip; do if [ -f "$zip" ]; then unzip "$zip" -d subs "*.srt" >/dev/null fi done # move all subtitles to subs catalog for srt in *.srt; do if [ -f "$srt" ]; then mv "*.srt" subs fi done mkvCount=(*.mkv) srtCount=(subs/*.srt) if [${#mkvCount[@]} != ${#srtCount[@]} ]; then echo "Different number of srt and mkv files!" exit 1 fi for MOVIE in *.mkv; do for SUBTITLE in subs/*.srt; do NAME=$(basename "$MOVIE" .mkv) SRT="subs/$NAME.srt" if [ ! -f "$SRT" ]; then echo "$SUBTITLE -> ${SRT}" mv "$SUBTITLE" "\$SRT" fi done done