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.


3 Answers 3


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 {} {#}
  • The first one assumes file names don't contain whitespace characters, quotes or backslashes (and if used in shells other than zsh, wildcard characters), don't start with -. The second that they don't start with - and don't contain newline characters that the braceccl or extendedglob (common) options are not enabled. You'd also want to use && to separate the two commands (and maybe -p for mkdir to still be successful if the directory already existed beforehand). Commented Oct 12, 2021 at 5:31

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).

  • Thanks! What does *(nN) do in the first line of the last example? Commented Feb 26, 2013 at 0:27
  • @user27915816, I've updated the answer. Commented Feb 26, 2013 at 7:18

There is a linux utility for that dirsplit https://linux.die.net/man/1/dirsplit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .