[英]Sort files to directories by date and filename
我得到的文件名总是以YM开头(例如2014-01),现在所有文件都在一个目录中,并且我想将它们拆分为根年目录(2014)和按月(01,02等)的子目录。 )
到目前为止,这是我手动执行的操作:
find /dirlocation/ -name "2014-12*" -type f -exec mv {} /pathtocp/2014/12 \;
而且我会每次手动更改日期和cp目录。
有人可以帮我提供一个bash脚本,以使其自动发生吗?
谢谢!
尝试运行:
for i in 2014 2015; do
for j in `seq 1 12`; do
j=`printf %.2d $j` #to convert 1 to 01
find /dirlocation/ -name "$i-$j*" -type f -exec mv{} /pathtocp/$i/$j \; #assuming this sentence is correctly written in question.
done;
done;
另一个解决方案:
#!/bin/bash
for f in $(find . -type f -regextype posix-extended -regex "./[0-9]{4}-[0-9]{2}.*"); do
y=${f:2:4}
m=${f:7:2}
mkdir -p "$y/$m" && mv "$f" "$y/$m/$f"
done
假设:
- bash脚本从文件所在的路径运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.