In what order are files and directories copied when using Linux cp -R?

11

1

If I execute the following command:

cp -R /myfiles /mydestination

If myfiles contains several sub-directories and files, in what order will they be copied?

For example, directories might be named 0123a, 9993c, myfolder, xfolder.

They are not copied in alphabetical order OR in date order OR in the order they appear when using a standard ls command as far as I can tell, so what actually does determine the order?

Edit: I am trying to determine the order that the cp command uses in order to determine how far along my copy command made it before it stopped. For example, I was hoping to be able to determine it copied 3 of the 4 directories successfully.

Scott Szretter

Posted 2011-06-26T12:48:10.327

Reputation: 1 433

Remember that the ls command by default sorts the directory contents before displaying them. What you see printed by ls is not the actual directory enumeration order. – JdeBP – 2011-06-26T14:09:19.243

1Use ls -U to get the unsorted directory order. This should be the order that cp uses. If you need more control over progress, order, etc., perhaps cp is not the tool for the job. – Daniel Andersson – 2012-05-11T08:16:05.127

Answers

2

The order that they're stored/returned in the filesystem. For "unsorted" filesystems this would generally be their creation order. For "sorted" filesystems (e.g. ext3/4 with dir_index) this would be the order used in the filesystem index.

Ignacio Vazquez-Abrams

Posted 2011-06-26T12:48:10.327

Reputation: 100 516

For unindexed filesystem formats it's a little more complex than that, if one has been deleting directory entries as well as creating them. (-: – JdeBP – 2011-06-26T14:05:02.643

11

We had EXACTLY the same problem. We copy large amounts of data to and from USB drives and it is annoying to be unable to tell how far through the process it has completed. We instead started using RSYNC.

Instead of using cp -Rv /source /destination

Use this instead rsync -av /source /destination

Now all of your folders and the contents of them will be processed in alphabetical order and you can easily see how the file copy is progressing.

We have a full description of this solution at our online backup website.

Moderators please note: I posted this once before but it got deleted, perhaps because I did not include enough detail in the answer, or because of the link. Maybe you'll accept this more detailed answer.

Michael Klimczak

Posted 2011-06-26T12:48:10.327

Reputation: 119