在Ubuntu18.10中,使用mmv将文件夹名(日期)放在多个文件夹中(“-n”标志用于查看结果):
mmv -n './????-??-??*/*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-#10.#11'例如,转换:
./2018-12-11/DSC05287.ARW -> ./2018-12-11/20181211-DSC05287.ARW它还皈依:
./2018-12-11/20181211-DSC05287.ARW -> ./2018-12-11/20181211-20181211-DSC05287.ARW同时维护时间戳。
有些文件已被重命名,有些文件没有重命名。
如何筛选命令以排除与模式匹配的文件--以8位数开头的文件,后面是连字符?
还是我需要一个不同的方法?
发布于 2019-02-11 16:39:07
我并不是一刀切的重命名实用程序的粉丝,所以我将只使用“标准设备”,特别是bash、find和mv来实现它。
假设您只对位于某个目录下的文件感兴趣(所以是find -maxdepth 2),特别是忽略以前已经重命名的文件(但是使用regex反向引用进行正常检查以避免假设):
find -regextype egrep -maxdepth 2 -type f \! -regex '\./([0-9]{4})-([0-9]{2})-([0-9]{2}).*/\1\2\3-.+' | while read f; do
if [[ $f =~ ^(\./([0-9]{4}-[0-9]{2}-[0-9]{2}).*)/(.+)$ ]]; then
# The above regex groups its matches into:
# ${BASH_REMATCH[1]} = the dir pathname
# ${BASH_REMATCH[2]} = the date in the dir pathname (remember to strip its dashes)
# ${BASH_REMATCH[3]} = the file name
mv -v "$f" "${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]//-}-${BASH_REMATCH[3]}"
fi
done并修复那些已经错误地“双日期”(同样,使用反向引用以避免不必要的假设):
find -regextype egrep -maxdepth 2 -type f -regex '\./([0-9]{4})-([0-9]{2})-([0-9]{2}).*/\1\2\3-\1\2\3-.+' | while read f; do
if [[ $f =~ ^(\./.+)/([0-9]{8})-[0-9]{8}-(.+)$ ]]; then
# The above regex groups its matches into:
# ${BASH_REMATCH[1]} = the dir pathname
# ${BASH_REMATCH[2]} = the first date chunk
# ${BASH_REMATCH[3]} = the filename "tail"
mv -v "$f" "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}-${BASH_REMATCH[3]}"
fi
done=~操作符,然后读取特殊的BASH_REMATCH数组find和locate支持的令人惊讶的正则表达式的详细信息。https://askubuntu.com/questions/1117389
复制相似问题