[英]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.