Take the 2-minute tour ×

I've got a web server which has files uploaded to it. There is a script which assigns them numeric IDs and stores them in a corresponding subdirectory.

I've now got 32000 of these uploads and that's too many for the server to handle in one directory. The script I'm using does have a way to "namespace" uploads so that ID 12345 - instead of sitting in /files/12345 - would sit in /files/namespaced/000/012/345.

The code can deal with this just fine, but I now have 32000 subdirectories in the wrong naming style. What's the easiest way to go through my existing files and put them in the right place?

share|improve this question
OS? Scripting language/shell? –  Dennis Williamson Jun 14 '10 at 11:13
add comment

1 Answer

up vote 1 down vote accepted

This assumes that the directory names don't have any odd characters such as spaces or newlines in their names and that there are no filenames that start with a dot.


find /files -type d | while read -r dir
    new="/files/namespaced/${base: -9:3}/${base: -6:3}/${base: -3}"
    mkdir -p "$new"
    mv "$dir/*" "$new"
    rmdir "$dir"

Another way to obtain the new directory name:

new=$(echo "$dir" | sed 's|...$|/&|;s|\(.*\)/\(.*\)/\(...\)$|\1/namespaced/000000\2/\3|; s|\(.*\)/.*\(...\)\(...\)/\(...\)$|\1/\2/\3/\4|' "$dir")
share|improve this answer
I changed to mv "$dir" "$new" and then rmdir was redundant, but looks good –  Gareth Jun 14 '10 at 14:07
Oh, and because of that I had to tweak the folder renaming, but that was simple enough –  Gareth Jun 14 '10 at 14:11
add comment

Your Answer


