Was wondering if someone could help me out with regular expressions and bash.
I’m trying to execute a set of commands on files that only have a certain extension, in this case: mpg, mpeg, avi, and mkv.
I’ve actually found a solution here, however, it doesn’t seem to work. If someone can tell me why, I’d appreciate it.
#!/bin/bash
# Configuration
TARGETDIR="$1"
TARGETEXT="(mpg|mpeg|avi|mkv)"
for d in `find $1 -type d`
do
echo "Searching directory: $d"
for f in "$d"/*
do
if [ -d "${f}" ];
then
# File is a directory, do not perform
echo "$f is a directory, not performing ..."
elif [ -f "${f}" ];
then
filename=$(basename "$f")
extension="${filename##*.}"
if [ "$extension" == "$TARGETEXT" ];
then
echo "Match"
else
echo "Mismatch - $f - $extension"
fi
fi
done
done
Again, any assistance is appreciated.
Instead of direct string comparison
use Bash regex matching syntax
Note the double [[ ]] and the non-quoted $TARGETEXT.