Why does cp -r copy unsorted?

  • Follow


When I copy music files from my hdd to my mp3 player they often play in
the "wrong" order on the player, even though the tracks are numbered
sequentially (as they were ripped from the CD).

When I checked with cp -rv <source> <dest> the output clearly showed the
tracks being copied in a seemingly random order:

`electric_light_orchestra/eldorado/' ->
`electric_light_orchestra/eldorado/07-nobodys_child.mp3' -> 
`electric_light_orchestra/eldorado/06-mister_kingdom.mp3' -> 
`electric_light_orchestra/eldorado/08-illusions_in_g_major.mp3' ->
`electric_light_orchestra/eldorado/12-dark_city_bonus_track.mp3' ->
`electric_light_orchestra/eldorado/03-boy_blue.mp3'
`electric_light_orchestra/eldorado/04-laredo_tornado.mp3' 
`electric_light_orchestra/eldorado/10-eldorado_finale.mp3'
.....
`electric_light_orchestra/eldorado/01-eldorado_overture.mp3' ->

The MP3 player is a flash device which plays tracks sequentially in the
order they were layed down, not in alphanumeric order - not good for this
particular album.

cp -v src/* dst/ works perfectly.  Any ideas why cp -r doesn't?
-- 
David Haggett
Linux user since 01/01/2003
Email: david<at>haggett<dot>demon<dot>co<dot>uk

0
Reply news-spam (68) 10/24/2003 6:49:16 AM

David Haggett <news-spam@haggett.demon.co.uk> wrote:
> When I checked with cp -rv <source> <dest> the output clearly showed the
> tracks being copied in a seemingly random order:

It just copy data from one directory to another, there is no reason
why entries in one directory have to be sorted in any specific way.
If you want them sorted, use a script that read the directory, sort
it and then copy the single files one by one.

Davide
0
Reply davideyeahsure (1281) 10/24/2003 6:55:54 AM


On Fri, 24 Oct 2003 06:55:54 +0000, Davide Bianchi wrote:

> David Haggett <news-spam@haggett.demon.co.uk> wrote:
>> When I checked with cp -rv <source> <dest> the output clearly showed the
>> tracks being copied in a seemingly random order:
> 
> It just copy data from one directory to another, there is no reason
> why entries in one directory have to be sorted in any specific way.
> If you want them sorted, use a script that read the directory, sort
> it and then copy the single files one by one.

Thanks for the input - what I suspected. Just wondered if there was an
easier way (such as an undocumented switch for cp)

Thanks again.

-- 
David Haggett
Linux user since 01/01/2003
Email: david<at>haggett<dot>demon<dot>co<dot>uk

0
Reply news-spam (68) 10/24/2003 7:04:29 AM

On 24 Oct 2003 06:55:54 GMT, Davide Bianchi <davideyeahsure@onlyforfun.net> wrote:
> 
> 
> David Haggett <news-spam@haggett.demon.co.uk> wrote:
>> When I checked with cp -rv <source> <dest> the output clearly showed the
>> tracks being copied in a seemingly random order:
> 
> It just copy data from one directory to another, there is no reason
> why entries in one directory have to be sorted in any specific way.
> If you want them sorted, use a script that read the directory, sort
> it and then copy the single files one by one.
> 
> Davide


Yeh. Something like:

 
#!/bin/sh

X=type_of_file   # see man find TESTS -type   probably f
Y=options_to_sort  # see man sort

find ./ -type $X  | sort -$Y > fileA

while read line ; do
cp  $line  destination-dir
done  < fileA

rm fileA

exit 0

-- 
Alan C
Post validation at http://tinyurl.com/rv0y
0
Reply zzzzzz (1897) 10/24/2003 7:29:04 AM

David Haggett <news-spam@haggett.demon.co.uk> wrote:
> On Fri, 24 Oct 2003 06:55:54 +0000, Davide Bianchi wrote:

>> David Haggett <news-spam@haggett.demon.co.uk> wrote:
>>> When I checked with cp -rv <source> <dest> the output clearly showed the
>>> tracks being copied in a seemingly random order:
>> 
>> It just copy data from one directory to another, there is no reason
>> why entries in one directory have to be sorted in any specific way.
>> If you want them sorted, use a script that read the directory, sort
>> it and then copy the single files one by one.

> Thanks for the input - what I suspected. Just wondered if there was an
> easier way (such as an undocumented switch for cp)

If you want a command to see several files in alphabetical
order, dont use cp * $dest - use something like cp `ls -v` $dest.

Whether or not the command will then preserve the order in the destination
location, e.g. an external mpeg player, is yet another question.


-- 
Disclaimer: everything I told you might be wrong.
0
Reply foth (74) 10/24/2003 7:33:34 AM

Kilian A. Foth <foth@informatik.uni-hamburg.de> wrote:
> If you want a command to see several files in alphabetical
> order, dont use cp * $dest - use something like cp `ls -v` $dest.

Either of these will sort alphabetically. (The * is expanded by the
shell before cp is invoked.)

Chris
0
Reply news262 (39) 10/24/2003 8:14:17 AM

On Fri, 24 Oct 2003 06:55:54 +0000, Davide Bianchi wrote:

> David Haggett <news-spam@haggett.demon.co.uk> wrote:
>> When I checked with cp -rv <source> <dest> the output clearly showed the
>> tracks being copied in a seemingly random order:
> 
> It just copy data from one directory to another, there is no reason
> why entries in one directory have to be sorted in any specific way.
> If you want them sorted, use a script that read the directory, sort
> it and then copy the single files one by one.
> 
> Davide

You don't have to write a script for this - you could do it on the command
line something like this:

s=/src/dir; d=/dst/dir; find $s -type f | sort | (while read f ; do cp $f
$d${f#$s}; done )

best to check this first by putting an 'echo' in front of the 'cp' though.

andy.

-- 
remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
bin unless notified with [html] or [attachment] in the subject line. 
0
Reply news37 (285) 10/24/2003 9:23:43 AM


David Haggett wrote:

> When I copy music files from my hdd to my mp3 player they often play in
> the "wrong" order on the player, even though the tracks are numbered
> sequentially (as they were ripped from the CD).
> 
> When I checked with cp -rv <source> <dest> the output clearly showed the
> tracks being copied in a seemingly random order:
> 
> `electric_light_orchestra/eldorado/' ->
> `electric_light_orchestra/eldorado/07-nobodys_child.mp3' -> 
> `electric_light_orchestra/eldorado/06-mister_kingdom.mp3' -> 
> `electric_light_orchestra/eldorado/08-illusions_in_g_major.mp3' ->
> `electric_light_orchestra/eldorado/12-dark_city_bonus_track.mp3' ->
> `electric_light_orchestra/eldorado/03-boy_blue.mp3'
> `electric_light_orchestra/eldorado/04-laredo_tornado.mp3' 
> `electric_light_orchestra/eldorado/10-eldorado_finale.mp3'
> ....
> `electric_light_orchestra/eldorado/01-eldorado_overture.mp3' ->
> 
> The MP3 player is a flash device which plays tracks sequentially in the
> order they were layed down, not in alphanumeric order - not good for this
> particular album.
> 
> cp -v src/* dst/ works perfectly.  Any ideas why cp -r doesn't?


"cp -r" reads the directory structure in the order that is found on the 
hard disk .. I dont know why the order is apparently random. maybe its 
do with the order that you did the ripping.

"cp -v src/* dst" is translated to
"cp -v src/file1 src/file2 src/file3 dst" by the shell,
and the shell works in alphabetical/numeric order, then the cp program 
runs through the list of source files in order.








0
Reply leon1 (3) 10/26/2003 8:27:40 AM

David Haggett <news-spam@haggett.demon.co.uk> wrote:
> When I copy music files from my hdd to my mp3 player they often play in
> the "wrong" order on the player, even though the tracks are numbered
> sequentially (as they were ripped from the CD).
> 
> When I checked with cp -rv <source> <dest> the output clearly showed the
> tracks being copied in a seemingly random order:

[ cp -v ]
 
> The MP3 player is a flash device which plays tracks sequentially in the
> order they were layed down, not in alphanumeric order - not good for this
> particular album.
> 
> cp -v src/* dst/ works perfectly.  Any ideas why cp -r doesn't?


the * is being expanded by the shell and your shell sourts the output.
if you cp src/ dest/ cp will read the filenames from the directory entry
on the filesystem.
another way to get fast sorted copying would be
find src/ | sort | cpio -p dest/

best regards, armin


-- 

my life, my universe, my everything
http://www.dtch.org

0
Reply geschrei (40) 10/26/2003 9:14:37 AM

8 Replies
37 Views

(page loaded in 0.134 seconds)


Reply: