2
!/bin/bash

# When a match is not found, just present nothing.
shopt -s nullglob

# Match all .wav files containing the date format.
files=(*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*.wav)

if [[ ${#files[@]} -eq 0 ]]; then
echo "No match found."
fi

for file in "${files[@]}"; do
# We get the date part by part
file_date=''
# Sleep it to parts.
IFS="-." read -ra parts <<< "$file"
for t in "${parts[@]}"; do
        # Break from the loop if a match is found
    if [[ $t == [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]]; then
        file_date=$t
        break
    fi
done
# If a value was not assigned, then show an error message and continue to the next file.
# Just making sure there is nothing in Array and date before it moves on
if [[ -z $file_date ]]; then

    continue
fi

file_year=${file_date:0:4}
file_month=${file_date:4:2}




mkdir -p "$file_year/$file_month"

# -- is just there to not interpret filenames starting with - as options.

echo "Moving: ./"$file "to: " "./"$file_year"/"$file_month
mv  "$file" "$file_year/$file_month"
done

现在有一些文件我需要做日期来标记日期,然后像现在一样移动它。例如,有一个名为 meetme 的文件。它是一个 wav 文件,我有 YYYY/MM 的 DIR,并且想移动那些文件名中没有 YYYYMMDD 的文件

4

1 回答 1

3

如果您正在编写一个程序来处理该信息,那么您可能更喜欢 seconds-since-epoch 并使用date以获取所需格式的日期。

$ date -d @$(stat --format='%Y' testdisk.log) +%Y%m%d
20130422

您还可以获取 ascii 表示,然后操作字符串

$ stat --format='%y' testdisk.log 
2013-04-22 09:11:39.000000000 -0500
$ date_st=$(stat --format='%y' testdisk.log)
$ date_st=${date_st/ */}
$ date_st=${date_st//-/}
$ echo ${date_st}
20130422
于 2013-10-07T14:28:16.447 回答