Bash – Split Large Directory

| | | | |

If you have a directory with a lot of files/directories in it and need it to be smaller this block will help by grouping 400 files at a time into different sub-directories.

Bash
#!/bin/sh

# macOS
i=0; for f in *; do d=dir_$(printf %03d $((i/400+1))); mkdir -p $d; mv "$f" $d; let i++; done

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.