Ubuntu – Moving multiple extension file name in another directory

bashmvscripts

#!/bin/bash
# script to find and move files

SOURCE=/DRIVE2/folder/
DESTDIR=/DRIVE/new3/

MOVEFILES=(mpg avi m4v mp4 3gp mpeg MOV) 
# this line above is not working, its only reading the first extension

find $SOURCE -type f -name *.$MOVEFILES -print | xargs -i mv -v "{}" $DESTDIR 

Can you guys help me with my bash script? The $MOVEFILES part of my script is not working. It's only reading the first extension I put there which is mpg. The rest is being ignored. What is the proper way to write that line?

Also is it possible to include the directory its from lets say
/DRIVE2/folder1/folder2/folder3/folder4/file.ext move to /DRIVE/folder3/folder4/file.ext

Best Answer

Using find with -regex

MOVEFILES=".*\.\(mpg\|avi\|m4v\)"
find "$SOURCE" -type f -regex "$MOVEFILES" -exec mv {} "$DESTDIR" \;

You could also use -iregex instead of -regex for case-insensitve matches like AvI and so on.

All the \ looks ugly, I know. -regextype posix-extended, as used in @Murus answer, looks better.

Related Question