Another Aide-mémoire – Renaming Files Based on Modification Time

I have a bunch of weather station time lapse daily web cam files (found here: Historical 24-hour WX Timelapse Videos) where the date of the web page index is the day after. This has bugged me for the past three years, but it was one of those things to “get around to”.

I finally got “around to it” (after getting tired of experimenting with pfSense haproxy for now). So that I will remember how I did it, my code is:

# Rename the file based on the modified date
# go through the list of .mp4 files
for file in *.mp4 ; do
# Part 1 - for each file get the modification date and put it in a string. $file needs double quotes 
# due to spaces in the original
        part1="$(stat -c %Y "$file")"
# Part 2 - for each file add the destination location, format the new filename based on creation time (YYYY-MONTH-DD) and add the ".mp4" extension  
        part2="fixed/$(echo "$part1" | awk '{ print strftime("%Y-%B-%d", $1)".mp4" }')"
# Uncomment this to test first :-)
#       echo $file " is now" $part2
# This is the actual move of the original file (double quotes due to the spaces in the filename) to 
# the new directory. Use --preserve so the modification date doesn't change
        cp --preserve "$file" $part2
# All done

It may not be the prettiest, most efficient code, but it works.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.