Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 3 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? –  user815423426 Feb 26 '13 at 0:27
@user27915816, I've updated the answer. –  Stephane Chazelas Feb 26 '13 at 7:18
add comment

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.