Vou postar mais duas soluções na esperança de que elas possam ser úteis no futuro. Estes vieram dos administradores do Linux no trabalho. Apenas vai mostrar quantos martelos vão trabalhar nessa unha!
Solução 1:
Hi Denham,
I'm having to make a few assumptions here, for instance that the part of the directory with "XXX Season #" will always be the "outside" directory (leaf node).
In any case, I would write a little script. Something like this should work (note the double quotes around the variables, to ensure you capture all the spaces in the directories):
find /media/Expansion2/Series/ -type d | while read olddir
do
newdir='echo "${olddir}" | awk -F "/" '$NF ~ /Season/ { last=substr($NF,index($NF, "Season")); while (i<(NF-1)) { i++; printf("/%s", $i) }; printf("/%s\n", last) } $NF !~ /Season/ { print }''
if [ "${olddir}" != "${newdir}" ]
then
mv "${olddir}" "${newdir}"
fi
done
Of course, before you run it with the command " mv "${olddir}" "${newdir}" " you should put something like " echo "${olddir}" "${newdir}" " to ensure you're getting the results you expect or you could end up with another headache :-P
Solução 2:
Hi Denham,
Most of the answer was already in the question. Anyway running something like the following from the Series folder should work just fine:
find -mindepth 2 -maxdepth 2 -type d | while read dir; do mv -T "$dir" "'dirname "$dir"'/'basename "$dir" | sed "s/.*Season \([0-9]*\)$/Season /i"'"; done
Explanation:
• find -mindepth 2 -maxdepth 2 -type d (list directories two levels down)
• while read dir; (loop on each dir)
• mv -T "$dir" (move the source dir to... -T is needed to get an error if the Season folders are not unique i.e. you don't have "The Big Bang Theory Season 22" and "Season 22" in the same directory)
• dirname "$dir" returns the path where the dir is
• basename "$dir" returns the name of the directory
• sed "s/.Season ([0-9])$/Season /i" completes the magic with case insensitive regexp, just in case.
In my small test it worked (try it first with an echo before mv):
someuser@linux-box:/tmp/Series$ find
.
./The Big Bang Theory
./The Big Bang Theory/Season 2
./The Big Bang Theory/Season 2/file1.avi
./The Big Bang Theory/Season 2/file 3.avi
./The Big Bang Theory/Season 2/file2.avi
./The Big Bang Theory/Season 2/file
./The Big Bang Theory/Season 2/3.avi
./The Big Bang Theory/The Big Bang Theory Season 1
./The Big Bang Theory/The Big Bang Theory Season 1/file1.avi
./The Big Bang Theory/The Big Bang Theory Season 1/file 3.avi
./The Big Bang Theory/The Big Bang Theory Season 1/file2.avi
./The Big Bang Theory/The Big Bang Theory Season 1/file
./The Big Bang Theory/The Big Bang Theory Season 1/3.avi
./Other Series
./Other Series/Season 2
./Other Series/Stre dsfdf sd dSeason 3
someuser@linux-box:/tmp/Series$ find -mindepth 2 -maxdepth 2 -type d | while read dir; do mv -T "$dir" "dirname "$dir"
/basename "$dir" | sed "s/.*Season \([0-9]*\)$/Season /i"
"; done
mv: ./The Big Bang Theory/Season 2' and
./The Big Bang Theory/Season 2' are the same file
mv: ./Other Series/Season 2' and
./Other Series/Season 2' are the same file
someuser@linux-box:/tmp/Series$ find
.
./The Big Bang Theory
./The Big Bang Theory/Season 2
./The Big Bang Theory/Season 2/file1.avi
./The Big Bang Theory/Season 2/file 3.avi
./The Big Bang Theory/Season 2/file2.avi
./The Big Bang Theory/Season 2/file
./The Big Bang Theory/Season 2/3.avi
./The Big Bang Theory/Season 1
./The Big Bang Theory/Season 1/file1.avi
./The Big Bang Theory/Season 1/file 3.avi
./The Big Bang Theory/Season 1/file2.avi
./The Big Bang Theory/Season 1/file
./The Big Bang Theory/Season 1/3.avi
./Other Series
./Other Series/Season 3
./Other Series/Season 2