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.

Is there a command to copy music files in order?

I need to copy MP3s which are in folders by "Artist - Album" to a USB stick.

The MP3s must be copied in order, for example 01 Can You Feel It.mp3 should be copied before 02 Skyway.mp3.

share|improve this question
2  
Just wondering. What is your actual use case? –  saji89 Oct 14 '12 at 9:22
add comment

3 Answers

You can't copy files in order with commandline or nautilus. But you can use midnight commander to copy some files in order. Install it on terminal with this command:

sudo apt-get install mc
share|improve this answer
    
thanks for the response. unfortunately i still can't figure how to copy in order. i've looked through some man pages and whatnot - am i missing something? –  Phillip Oct 7 '12 at 19:02
add comment

If you can't find any ready-to-use software to do this, you can very easily write one.

In python, a script to recursively copy all files with there respective folders to a new folder in order, could look like this:

#/usr/bin/env python

import os
import sys
import shutil

try:
    from_path = os.path.expanduser(os.path.expandvars(sys.argv[1]))
    to_path = os.path.expanduser(os.path.expandvars(sys.argv[2]))
except:
    print "Usage: copyinorder <from path> <to path>"
    exit()

if not os.path.exists(to_path) or not os.path.exists(from_path):
    exit("Error: both paths must exist.")

for dir_path, dir_names, file_names in os.walk(from_path):
    if file_names:
        new_dir_path = os.path.join(to_path, os.path.relpath(dir_path, from_path))
        if not os.path.exists(new_dir_path):
            os.makedirs(new_dir_path)
        for name in sorted(file_names):
            dest = os.path.join(new_dir_path, name)
            src = os.path.join(dir_path, name)
            shutil.copyfile(src, dest)
            print "copied {0} \n to {1}".format(src, dest)

You could just save it somewhere (e.g. as "copyinoder"), make it executable (e.g. chmod +x copyinorder) and run it like this:

copyinorder ~/Music/DavidBowie /media/N900/Music/

Note that in this case the "DavidBowie" folder will not be recreated.

Note also, that if the files are not as nicely named as in your example, then sorted might not be the order you wanted.

share|improve this answer
add comment

This bit of code should do what you're looking for. Modify the sort portion to alter the sorting behaviour to taste.

for i in "$(ls *.mp3 | sort)"; do cp "$i" "/destination/$i"; done

ETA: This might only work on a single directory at a time, but you can try ls -R, though I don't know if that would copy recursively.

share|improve this answer
add comment

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.