嗨德纳姆,
我在这里必须做一些假设,例如,带有“ XXX Season#”的目录部分将始终是“外部”目录(叶节点)。
无论如何,我都会写一个小脚本。这样的事情应该起作用(注意变量周围的双引号,以确保捕获目录中的所有空格):
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
当然,在使用命令“ mv” $ {olddir}“” $ {newdir}“”“来运行它之前,您应该输入” echo“ $ {olddir}”“ $ {newdir}”“这样的内容,以确保您得到您期望的结果,否则您可能会再次头痛:-P
嗨德纳姆,
大多数答案已经在问题中了。无论如何,从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 \1/i"`"; done
说明:
•find -mindepth 2 -maxdepth 2 -type d(列出目录向下两级)
。(在每个目录上循环)
•mv -T“ $ dir”(将源目录移动到...-如果Season文件夹不是唯一的,则错误-T是必需的,即您没有“ The Big Bang Theory Season” 22“和” Season 22“在同一目录中)
•dirname” $ dir“返回目录所在的路径
•basename” $ dir“返回目录名称
•sed” s /。Season([0-9])$ / Season \ 1 / i“以不区分大小写的正则表达式来完成魔术,以防万一。
在我的小型测试中,它可以正常工作(先在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 \1/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