I have a directory called "images" filled with about one million images. Yep.
I want to write a shell command to rename all of those images into the following format:
original: filename.jpg
new: /f/i/l/filename.jpg
Any suggestions?
Thanks,
Dan
You can generate the new file name using, e.g., sed:
So, you can do something like this (assuming all the directories are already created):
or, if you can't use the bash
$(
syntax:However, considering the number of files, you may just want to use perl as that's a lot of sed and mv processes to spawn:
That perl is surely limited to same-filesystem only, though you can use
File::Copy::move
to get around that.I suggest a short python script. Most shell tools will balk at that much input (though xargs may do the trick). Will update with example in a sec.
Any of the proposed solutions which use a wildcard syntax in the shell will likely fail due to the sheer number of files you have. Of the current proposed solutions, the perl one is probably the best.
However, you can easily adapt any of the shell script methods to deal with any number of files thus:
I would still use perl, but if you're limited to only having simple unix tools around, then combining one of the above shell solutions with a loop like I've shown should get you there. It'll be slow, though.
The
${i:0:1}/${i:1:1}/${i:2:1}
part could probably be a variable, or shorter or different, but the command above gets the job done. You'll probably face performance issues but if you really want to use it, narrow the*.*
to fewer options (a*.*
,b*.*
or what fits you)edit: added a
$
beforei
formv
, as noted by DanYou can do it as a bash script:
Needless to say, you might need to worry about spaces and the files with short names.