Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

I have a folder A with hundreds of thousands of files. I would like to move these files to new subfolders S_i, with, say, 100 files in each (the last folder may have less than 100 files)

In other words, if my folder A has:


I would later have:


Before I write a Zsh script manually to do this task (e.g. using variables to count files), I was wondering if there are any readily available tools (like split) that would facilitate this task.

share|improve this question
I think this is the best answer: stackoverflow.com/questions/17669176/… – Robin Winslow Oct 8 '15 at 16:23
up vote 4 down vote accepted

With zsh:

autoload zmv
zmv 'file_(<->)' 'S_$((1 + ($1 - 1) / 100))'

If the files are not numbered, but you just want to split that list:

n=0; zmv -Q 'file_*(n)' 'S_$((n++/100+1))'

(n) is to toggle numerical ordering for the list (and you need -Q for that globbing qualifier).

Those call one mv per file. You can make it a bit more efficient by making mv builtin (zmodload zsh/files), or you could do:

for ((n=1; $#files; n++)) {
  mv -- $files[1,100] S_$n

The (nN) above are zsh globbing qualifiers which further qualify the glob. n is for numerical sorting, N is to turn on the nullglob option for that glob, that is for the glob to expand to an empty list when there is no match.

(with that one, you could even throw in a mkdir S_$n in case those directories didn't exist beforehand).

share|improve this answer
Thanks! What does *(nN) do in the first line of the last example? – Amelio Vazquez-Reina Feb 26 '13 at 0:27
@user27915816, I've updated the answer. – Stéphane Chazelas Feb 26 '13 at 7:18

If the filenames don't contain spaces:

i=1;while read l;do mkdir $i;mv $l $((i++));done< <(ls|xargs -n100)

Using parallel:

ls|parallel -n100 mkdir {#}\;mv {} {#}
share|improve this answer

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.