Moving all files of a certain type or extension

I have a large external with many subfolders that I want to move all videos, pictures, and music to another hard-drive, what would be the best way to do this?


I'm not really sure you want to move many files from many directories but here's the basic command for finding files of multiple extensions in one directory structure:

find /path/to/look/ -type f \( -iname \*.mp3 -o -iname \*.avi -o -iname \*.mp4 \)

You'll obviously want to tack more -o -iname ... onto that until you're sure you're finding all the files you want. When you've got everything, just add this to the end:

-exec mv -t /path/to/destination {} +

And that'll move them all into the specified directory.


A Bashier way of doing this would be using a few globbing modifiers (globstar to recursive search with **, nullglob to only return valid files and nocaseglob to ignore case) and then just passing them off to mv in a simple for loop:

shopt -s globstar nullglob nocaseglob
for f in /path/to/look/{**/,}*.{mp3,avi,mp4}; do
  mv "$f" /path/to/destination/
done

I would try something like..

for files in $(find / | grep -P "([.]mp3$)|([.]avi$)")
do
cp $files /media/<your_external_drive>/target_folder/
done

You have to add every extension you want to search for in the regex if you want to do it all in one shot.

Or if you simply want to move the directories, you could use,

cp -r /target/dir/on/machine /target/dir/on/external/drive

for each directory containing your media files.