Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trying to import 53,800+ individual files (messages) using Gmail's POP fetcher.

Gmail understandably refuses, giving the error: "Too many messages to download. There are too many messages on the other server."

The folder in question looks like similar to:


Using the shell (tcsh, sh, etc. on FreeBSD), what one-line command can I type to split this directory full of files into separate folders so Gmail only sees 1000 messages at a time? Something with find or ls | xargs mv maybe. Whatever is fastest.

The desired output directory would now look something like:

/usr/home/customer/set1/ (contains messages 1-1000)
/usr/home/customer/set2/ (contains messages 1001-2000)
/usr/home/customer/set3/ (etc.)
share|improve this question
up vote 4 down vote accepted

It's not a one-liner, but you could copy chunks like this (in csh):

foreach file (`ls | head -n 1000`)
mv $file /tmp/new/dir

I'm not 100% sure that pipe will work with the number of files you've got, but it's worth a shot. Also, you might be able to do 500 at a time with this command, just change that 1000 to 500.

share|improve this answer
Thanks very much Chris - this worked great. – ane Mar 11 '10 at 20:14
in bash: for file in (ls | head -n 1000) do mv $file /tmp/new/dir; done – warren Mar 11 '10 at 23:09
Thanks Warren. That will work too. – ane Mar 12 '10 at 15:27

find srcdir/ -type f |
    while read file; do


        [ -d $target ] || mkdir $target

        echo mv "$file" $target; #remove the 'echo' if you like what you see

collapsed to a single line (and with the 'echo' safeguard removed):

count=target=0; find srcdir/ -type f | while read file; do count=$((count+1)); target=$((count/10000)); [ -d $target ] || mkdir $target; mv "$file" $target; done

This isn't the fastest but it's clean. this solution avoids parsing the output of 'ls' and quotes the references to "$file" otherwise files with abnormal names (as Maildir files often are) would break the code. For example if any of the files have spaces or semicolons then referencing $file without quotes isn't going to get you far (most of the time)

Read more about quoting: and

share|improve this answer
Thanks Stuart ! – ane Jun 16 '10 at 12:39

Your Answer


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.