I've got a series of files that are namedHHMMSSxxxxxxxxxxxxxxxx.mp3, where HH,MM, and SS are parts of a timestamp and the x's are unique per file.
The timestamp follows a 24 hour form (where 10am is 100000, 12pm is 120000, 6pm is 180000, 10pm is 220000, etc). I'd like to shift each down by 10 hours, so that 10am is 000000, 12pm is 020000, etc.
I know basic BASH commands for renaming and moving, etc, but I can't figure out how to do the modul开发者_运维知识库ar arithmetic on the filenames.
Any help would be very much appreciated.
#!/bin/bash
for f in *.mp3
do
printf -v newhour '%02d' $(( ( 10#${f:0:2} + 14 ) % 24 ))
echo mv "$f" "$newhour${f:2}"
done
Remove the echo
to make it functional.
Explanation:
printf -v newhour '%02d'
- this is likesprintf()
, the value is stored in the named variable$(( ( 10#${f:0:2} + 14 ) % 24 ))
-10#
forces the number to base 10 (e.g.08
would otherwise be considered an invalid octal),${f:0:2}
extracts the first two characters (the hour), the rest does the math"$newhour${f:2}"
- prepend the new hour before the substring of the original name, starting at the third character
The easiest way is probably to extract the timestamp and use date
to turn it into a number of seconds, do normal math on the result, then convert it back. date -d datestring +format
lets you do these conversions.
精彩评论