File Management – Move Files Based on Filename into Year/Month/Day Directory

filesmvtimestamps

I have files in the format YYYY_MM_DD_HH:MM:SS.swf that dump into a folder /home/user/dump/

I want to move these files into a new directory tree /home/user/save/year/month/day/ based on the YYYY_MM_DD from the filename. Alternatively, if these can be modified by the file modification date, that is acceptable as well. I've found a couple other scripts on here, but they don't seem to have all the info I'm looking for.

Best Answer

while read file
 do 
     f=$(basename $file)
     year=$(echo "$f"|cut -f1 -d_)
     day=$(echo "$f"|cut -f3 -d_)
     month=$(echo "$f"|cut -f2 -d_)
     new_dir="/home/user/save/$year/$month/$day"
     mkdir -p "$new_dir"
     mv "$file" "$new_dir"
done < <(find /home/user/dump -type f -name "*_*_*_*:*:*.swf")
Related Question