Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
    
That looks similar to what I'm trying to do, only I would like to have it move all found files to another directory. Sorry, I am very new to command line and I'm not sure how to change that to do it myself. –  Daniel Aug 8 at 20:05

4 Answers 4

I highly recommend the program FreeFileSync. It is great for not only syncing two directories, but also for the task you have in mind. You can add filters to include or exclude patterns. First you just browse to the origin on the left and the destination on the right. Then click compare to search through all your files. Finally click on the red funnel at the top to edit the filters.

For the filters, you place each pattern on a new line. You can replace the * in the includes section with something like the following and leave the excludes section untouched. The following will move only mp3, avi, and jpg files:

*.mp3
*.avi
*.jpg

Or, if you leave the * in the includes section, you can say what files you don't want to move. The following added to the excludes section will move everything except txt, odt, and pdf files:

*.txt
*.odt
*.pdf

Once you make your changes and close the filter dialog, files that aren't going to be moved will be grayed out. The buttons at the bottom next to "Select View:" allow you to change what files are displayed (these buttons don't affect what files are going to be moved). Displaying only the "Do nothing" files by having only the gray bar selected, look for any you want to be moved that aren't going to be, and modify filters accordingly. Likewise, displaying only the files to be moved by having only the green arrow selected, look for any that would be moved but you don't want to be and modify filters accordingly.

share|improve this answer

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.

share|improve this answer
    
This looks perfect, now how do I specify in find to tell it where to look? –  Daniel Aug 8 at 20:09
    
you want to say find /path/to/your/target/directory, where path to your target directory is something like /home/username/Music, or /home/username/Video. the instance above where I used find /, specifies that find should iterate through the entire available file system, which is pretty much always unnecessary, but the output is pretty. –  knotech Aug 8 at 20:18
    
Are those brackets and quotes right? –  Tim Aug 8 at 21:41
    
They are now, good catch. –  knotech Aug 8 at 21:43

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
share|improve this answer

You could use a simple python script to do the job:

#!/usr/bin/env python3

import os
import shutil

source_dir = "/path/to/files"; target_folder = "/path/to/destination"; extensions = (".mp3", ".avi")

for root, dirs, files in os.walk(source_dir):
    for name in files:
        if name.endswith(extensions):
            file = root+"/"+name
            shutil.move(file, target_folder+"/"+name)

Paste the code above in an empty textfile, enter the target- and source directories, enter the file extensions you want to move, save it as move.py and run it by the command:

python3 /path/to/move.py

To avoid overwriting possible duplicates (and rename them)

If there is a risk that the directory contains duplicates (names) that you want to keep, the code is a bit more extensive:

#!/usr/bin/env python3

import os
import shutil

source_dir = "/path/to/files"; target_folder = "/path/to/destination"; extensions = (".mp3", ".avi")

dup = 1
for root, dirs, files in os.walk(source_dir):
    for name in files:
        if name.endswith(extensions):
            file = root+"/"+name
            while os.path.exists(target_folder+"/"+name):
                name = name[:name.rfind(".")]+"_"+str(dup)+name[name.rfind("."):]
                dup = dup+1
            shutil.move(file, target_folder+"/"+name)

In this case, multiple occasions of file.mp3 will be renamed to:

file.mp3
file_1.mp3
file_2.mp3
...etc.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.