So I have this folder of movies that looks like
Current
movie1 (1962).mkv
movie 2b (1993)
movie 2b (1993) cd1.mp4
movie 2b (1993) cd1.srt
movie 3 (2019).avi
movie 1942 (2012).mkv
And I would like to move them into another folder that contains the folder structure by year with the right movies in each folder. If there is a sub-folder like with movie 2b I don't mind of that folder is stripped or kept - whatever is easier as long as it goes into the right year. The source and destination are in the same partition so I would rather use mv than rsync.
Sorted
1962
1993
2012
2019
I got this far based on another question/answers in the forum but I know it's wrong because I don't know how to strip the brackets from the year and I don't know how to specify a source and destination folder.
for f in *.*; do
if [ -f "$f" ] # does file exist?
then
dir=$(echo "$f" | grep -o "([0-9][0-9][0-9][0-9])" | head -1)
if [ "$dir" ] # check if string found
then
mkdir -p "$dir" # create dir
mv "$f" "$dir" # move file into new dir
else
echo "INCORRECT FILE FORMAT: \""$f"\"" # print error if file format is unexpected
fi
fi
done
Any suggestions?
Using BASH_REMATCH to backreference matched subexpression: