创建DIR的工作时间是一半

时间:2013-09-17 15:14:13

标签: bash

#!/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.
  if [[ -z $file_date ]]; then
    echo "Unable to find date string in $file."
    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.
  mv  "$file" "$file_year/$file_month"
done

当我为这种类型的文件OUT340-20130529-153516-1369856116.1254989.wav q1064-20130508-234158-1368070917.885627.wav运行此代码时...等等一半适用于所有带有年份和月份201306的文件..... wav它创建了DIR 2013年和月DIR 06完美,任何月份都高于06 ......但是如果我有一个月份低于06的文件,例如201305它会创建一个DIR 201305而不是DIR 2013然后DIR 05 ..它会正确地移动文件。请帮助。

more files....
OUT398-20130624-151423-1372101263.1793982.wav  q1200-20130628-212505-1372469104.1866114.wav
OUT398-20130624-151530-1372101330.1793991.wav  q1200-20130628-213046-1372469445.1866233.wav
OUT398-20130624-151820-1372101500.1794016.wav  q1200-20130629-120812-1372522091.1882860.wav
OUT398-20130624-152213-1372101733.1794045.wav  q1200-20130629-200936-1372550975.1895662.wav
OUT398-20130624-152442-1372101881.1794067.wav  q1200-20130629-225834-1372561113.1900285.wav
OUT398-20130624-152921-1372102161.1794079.wav  q1200-20130629-233440-1372563279.1901149.wav
OUT398-20130624-153535-1372102535.1794097.wav  q1200-20130629-235905-1372564744.1901865.wav
OUT398-20130624-153941-1372102781.1794112.wav  q1200-20130630-005644-1372568202.1902811.wav
OUT398-20130624-154551-1372103151.1794129.wav  q1200-20130630-010445-1372568684.1902846.wav
OUT398-20130624-155246-1372103566.1794159.wav  q1200-20130630-185852-1372633131.1926286.wav
OUT398-20130625-120223-1372176143.1808796.wav  q1200-20130630-194016-1372635615.1927485.wav
OUT398-20130625-120541-1372176341.1808803.wav  q1200-20130630-214058-1372642857.1930370.wav

OUT398-20130508-131952-1368033592.879814.wav   q1200-20130525-045719-1369472238.1165178.wav
OUT398-20130508-132411-1368033851.879856.wav   q1200-20130525-180457-1369519496.1177104.wav
OUT398-20130508-132604-1368033964.879859.wav   q1200-20130527-022816-1369636094.1212356.wav
OUT398-20130508-132735-1368034055.879867.wav   q1200-20130528-052751-1369733270.1238938.wav
OUT398-20130508-132937-1368034177.879871.wav   q1200-20130528-212945-1369790984.1246272.wav
OUT398-20130508-133627-1368034587.879886.wav   q1200-20130529-001535-1369800934.1248760.wav
OUT398-20130508-133841-1368034721.879890.wav   q1200-20130529-020008-1369807207.1249977.wav
OUT398-20130508-134227-1368034947.879900.wav   q1200-20130529-043239-1369816358.1251628.wav
OUT398-20130508-134412-1368035052.879904.wav   q1200-20130529-212332-1369877011.1259076.wav
OUT398-20130508-134850-1368035330.879930.wav   q1200-20130530-033837-1369899516.1263701.wav
OUT398-20130508-135101-1368035461.879940.wav   q1200-20130530-053741-1369906660.1264667.wav
OUT398-20130508-135334-1368035614.879954.wav   q1200-20130530-054224-1369906943.1264679.wav
OUT398-20130508-135543-1368035743.879962.wav   q1200-20130530-190816-1369955295.1270565.wav
OUT398-20130508-135756-1368035876.879966.wav   q1200-20130530-201623-1369959382.1272134.wav
OUT398-20130508-135945-1368035985.879972.wav   q1200-20130531-054907-1369993746.1279860.wav
OUT398-20130508-140155-1368036115.879988.wav   q1200-20130531-183331-1370039610.1286278.wav
OUT398-20130508-140706-1368036426.880020.wav   q1200-20130531-192717-1370042835.1287645.wav
OUT398-20130508-141859-1368037139.880068.wav   q1200-20130531-195250-1370044368.1288042.wav
OUT398-20130508-142116-1368037276.880072.wav   q1200-20130531-211655-1370049413.1290684.wav
OUT398-20130508-142240-1368037360.880099.wav   q1200-20130531-212413-1370049852.1290854.wav
OUT398-20130508-142459-1368037499.880108.wav   q1200-20130531-230851-1370056129.1293922.wav

0 个答案:

没有答案