GNU nano 2.2.6 File: years.sh
#!/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 file"
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 语句之前,如果对具有 YYYYMMDD.wav 格式的文件非常有效。
if [[ -z $file_date ]]; then
# this print $6 could be print $5 or $7 depending on your OS
another_file_date=`ls -l --time-style=long-iso | awk '{print $6}' | tr -d '-'`
file_year=${file_date:0:4}
file_month=${file_date:4:2}
mkdir -p "$file_year/$file_month"
echo "Moving: ./"$file "to: " "./"$file_year"/"$file_month
mv "$file" "$file_year/$file_month"
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 文件。我有一个名为 meetme93238927832783.wav 的文件...我想获取修改日期或文件的创建时间,并将 mive 移动到它年/月的 DIR 中。当我运行此代码时,它只是说没有文件是我的第一个 if 语句......现在在我添加新的 if 语句之前它可以正常使用 YYYYMMDD.wav 格式