1
#!/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
    unset $files
    continue
fi

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

echo $file_year
echo $file_month
echo mkdir -p $file_year/$file_month
mkdir -p "$file_year/$file_month"

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

echo "$file" "$file_year/$file_month" 
mv  "$file" "$file_year/$file_month"
done

したがって、私のコードを実行した後、私の出力はこれで、DIR 201211、201212、201301、201302、201303、201304がわかりました。YYYYMMDDを持つファイルがあり、DIRを年にしたいので、これを実行してみます。月、ファイル。時々、DIR2013/06/files を取得できますが、奇妙な理由で DIR201304/files を取得します。私は問題が何であるかを理解していません.......私は約1.1テラバイトでこのスクリプトを実行しています

4

1 に答える 1