The code below assumes that the filenames do not contain linefeeds, spaces, tabs, single quotes, double quotes, or backslashes, and that filenames do not start with a dash. It also assumes that IFS
has not been changed, because it uses while read
instead of while IFS= read
, and because variables are not quoted. Add setopt shwordsplit
in Zsh.
i=1;while read l;do mkdir $i;mv $l $((i++));done< <(ls|xargs -n2000)
The code below assumes that filenames do not contain linefeeds and that they do not start with a dash. -n2000
takes 2000 arguments at a time and {#}
is the sequence number of the job. Replace {#}
with '{=$_=sprintf("%04d",$job->seq())=}'
to pad numbers to four digits.
ls|parallel -n2000 mkdir {#}\;mv {} {#}
The command below assumes that filenames do not contain linefeeds. It uses the implementation of rename
by Aristotle Pagaltzis which is the rename
formula in Homebrew, where -p
is needed to create directories, where --stdin
is needed to get paths from STDIN, and where $N
is the number of the file. In other implementations you can use $.
or ++$::i
instead of $N
.
ls|rename --stdin -p 's,^,1+int(($N-1)/2000)."/",e'