I have a directory full of files stored in subdirectories according to their filename, i.e.:20d1/d325/52d1/20d1d32552d1a95249e62662fbdf924dd72c4027.jpgccaf/13cf/3199/ccaf13cf319930e80f5f2ad02525b93e1326c160.jpgec07/53bd/2355/ec0753bd2355fa8ec5cf5163e219c162cce3b03a.jpg...As you can see, the first 12 characters of the filename are used to create three levels of subdirectories. Unfortunately four characters per directory name were chosen, and as it happens the amount of files has grown beyond the directory limit of 32000 entries per directory on the filesystem. So they need to be rewritten to:20d/1d3/255/2d1/20d1d32552d1a95249e62662fbdf924dd72c4027.jpgcca/f13/cf3/199/ccaf13cf319930e80f5f2ad02525b93e1326c160.jpgec0/753/bd2/355/ec0753bd2355fa8ec5cf5163e219c162cce3b03a.jpg...So three letters per directory instead of four. There are a ton of files, so the process should be as fast as possible.I have dabbled around with find:find /path/to/files -mindepth 4 -type f -regextype posix-extended -regex \.*/([0-9a-f]{4}/){3}(([0-9a-f]{3})([0-9a-f]{3})([0-9a-f]{3})([0-9a-f]{3})[0-9a-f]+\.\w+) This prints all the files nicely, but I'm not sure how to proceed with the rewriting. I'd like to use the regex capture groups in the rewrite process to rewrite the path to $3/$4/$5/$6/$2 (backreferences to find regex). But find doesn't seem to support something like:find ... -exec cp {} /elsewhere/$3/$4/$5/$6/$2 ;What is the best approach to handle this? Some combination of sed and xargs (I'm not very experienced with those)? Should I go for a loop instead of a find action? I'm a little lost.
mv files to a different path which is constructed via a regex replacement
find;regular expression;rename
For copying the files you could use a combination of find and GNU tar for the job:$ find -type f ... -print0 \    | tar -c -f - --null --files-from - \    | tar -C DEST_BASE -v -x -f - \        --show-transformed \        --transform 's,PATTERN,REPLACE,OPTIONS(find generates all source file name, the first tar reads them into a pipe and the 2nd tar does the filename/path transformation)By default the --transform option expects a basic regular expression, but there is also the x regexp-option available. Another useful regexp-option is i for case-insensitive matching.