Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I had a look at the cp man page and I still cannot figure it out. It probably is something stupid or noob that I haven't thought of but if I type the command
cp -R * source dest
how does it copy the files? As in how does it decide which order they are copied in.
I am trying to copy about 20 folders onto a usb stick. I can copy them no bother. I was using Nemo in Linux Mint 17 and it works perfectly for most situations. The problem occurs when I try and play the files on my car stereo. For some reason the car stereo orders the tracks as they are copied onto the stick which is weird. So when I play a folder it starts off with what seemed like some random file instead of track 1. And the rest of the tracks were all mixed up in order. Not very important but annoying!
I thought it was the tags until I ruled that out. Then I thought it was spaces in the filenames and I ruled that out too. I decided then to copy the files manually and individually onto the usb stick and they copied fine, played fine and in the right order. So when I made the folder myself with mkdir, copied them individually with cp it worked perfectly.
So then I thought it was Nemo, so I copied them with the CP command with the '*' wildcard thinking it would be hunky dory but with the -v parameter it shows the files being copied in what seems to be a random manner. The files are named with a zero padded number at the start to display them in the right order e.g.
So when I use the list command 'ls -l' it shows them in the right order. So when the car stereo seemed to be playing the files in a random order it was in fact playing them in the order that cp copied them. It started with 05, 02, 09, 10, 03 etc.
So my question is. Is there some file attribute that CP uses to pick the first file? Or is there a way to force it to copy alphabetically? It shouldn't make any difference for 99% of the time to be fair but it is super annoying.
I realise it's not really a noob quesiton but maybe someone can clear it up. I guess i can use the find command to exec the cp command as a quick workaround.
Oh and just to say the USB stick is formatted as FAT32.
I had a look at the cp man page and I still cannot figure it out. It probably is something stupid or noob that I haven't thought of but if I type the command
cp -R * source dest
how does it copy the files? As in how does it decide which order they are copied in.
That command probably does not do what you intended. First, your shell will expand the "*" to a list of all the files and subdirectories in your current working directory. So, the cp command will first copy all of those, recursing into any subdirectories, and then start recursing into the "source" directory. All of that will end up in the "dest" directory.
Quote:
I am trying to copy about 20 folders onto a usb stick. I can copy them no bother. I was using Nemo in Linux Mint 17 and it works perfectly for most situations. The problem occurs when I try and play the files on my car stereo. For some reason the car stereo orders the tracks as they are copied onto the stick which is weird. So when I play a folder it starts off with what seemed like some random file instead of track 1. And the rest of the tracks were all mixed up in order. Not very important but annoying!
Most filesystems do not store directory entries in sorted order. For primitive directories like FAT, each new file is placed in the first empty slot, so reading the directory will return the names largely in the order they were placed there, though deleting a file and re-using that slot for another name can affect that. More advanced directories store the names in a database, and simply reading the names sequentially can show them in an unpredictable order, though at least one filesystem (and I forget which) does return the names in alphanumeric sequence.
Quote:
So then I thought it was Nemo, so I copied them with the CP command with the '*' wildcard thinking it would be hunky dory but with the -v parameter it shows the files being copied in what seems to be a random manner.
Oh and just to say the USB stick is formatted as FAT32.
That's because the "*" isn't being expanded where you think it is. Try
Code:
cp source/* dest
It's not going to descend into any subdirectories of "source", but it should copy the files in sequence (your shell did the sorting). If the FAT32 directory was previously empty, then that's the order in which they should appear.
I don't know of any way to ensure order when using "cp" recursively. There is no sorting available when it recurses into a directory.
well i don't know what happened but after googling 'cp alphabetical linux' etc. it seems this is a fairly common occurrence. I thought I had a good look for an answer but didn't. It's not a linux specific or 'cp' specific but just the way the car stereo reads the usb stick. i have a workaround by using 'rsync -av' but i would rather not copy everything via the command line.
anyone know is there a way to resort the files after i copy them with Nemo or can I change the behaviour of Nemo to copy them alphabetically?
thanks rknichols, that command worked great. It copied them over alphabetically. I still don't understand the reason behind it though. Is it to do with the time the original files were copied? Is there a way to edit the timestamp or is it the physical location of the file on the disc? So then if I move them to another folder alphabetically will they be sorted?
Your shell is expanding the "source/*" to a list of everything in that directory, "source/01 - I Remember Now.mp3", "source/02 - Anarchy-X.mp3", "source/03 - Revolution Calling.mp3", ... . The shell always performs an alphabetical sort of the result from pathname expansion, so the "cp" command sees the names in alphabetical order.
I tried the cp command for the whole music folder but like you mentioned it sorts the folders alphabetically but not the files inside.
cp -v -R /source/* /dest/
i will probably have to learn how to write a script to copy the whole lot or copy the folders one by one.
On a different note I did use rsync -var to copy them over onto the usb stick but unfortunately I will have to copy them all over in one go because adding folders and files later will mean no more alphabetically sorting. I presume this goes for the cp script if I ever get round to making it. Might be easier to get a new stereo!
As I said, I don't know of any way to ensure order when using "cp" recursively. Re-sorting the destination directory once the files are already there is going to be a mess unless there is some tool to do that specifically, and I don't know of one.
If the files are in various subdirectories, I believe the simplest way to handle it would be to create a temporary directory with symlinks to all the files, use "cp -L tmpdir/* /dest" to copy them, and then delete the temporary directory. Something like this should work:
Note that "/source" must be an absolute path (begins with "/") or the symlinks will not be valid. The "-L" option on the cp command will cause it to follow the symlinks and copy the actual files. Note that the ln command will complain about any duplicate names.
NO!
if you use the '-R' option, you don't need the wildcard '*'!!!
that is what wreaks havoc with your stuff, because the shell tries to expand it BEFORE the cp command reads it!
NO!
if you use the '-R' option, you don't need the wildcard '*'!!!
The source directory contains subdirectories, so recursion is required. The problem is that alphabetical order is not preserved during recursion, and that's going to be true in any filesystem if there are multiple subdirectories since each subdirectory is going to be processed completely before moving on to the next. You need to get all the filenames in one place so that they can be sorted.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.