You can generate a new file name using, for example, sed:
$ echo "test.jpg" | sed -e 's/^\(\(.\)\(.\)\(.\).*\)$/\2\/\3\/\4\/\1/' t/e/s/test.jpg
So you can do something like this (assuming all directories are already created):
for f in *; do mv -i "$f" "$(echo "$f" | sed -e 's/^\(\(.\)\(.\)\(.\).*\)$/\2\/\3\/\4\/\1/')" done
or if you cannot use bash $( syntax $( :
for f in *; do mv -i "$f" "`echo "$f" | sed -e 's/^\(\(.\)\(.\)\(.\).*\)$/\2\/\3\/\4\/\1/'`" done
However, given the number of files, you can simply use perl, since there are many sed and mv processes to create:
#!/usr/bin/perl -w use strict; # warning: untested opendir DIR, "." or die "opendir: $!"; my @files = readdir(DIR); # can't change dir while reading: read in advance closedir DIR; foreach my $f (@files) { (my $new_name = $f) =~ s!^((.)(.)(.).*)$!$2/$3/$4/$1/; -e $new_name and die "$new_name already exists"; rename($f, $new_name); }
This perl is certainly limited only to the file system, although you can use File::Copy::move to get around this.
derobert
source share