{"id":806,"date":"2021-04-09T20:59:59","date_gmt":"2021-04-10T00:29:59","guid":{"rendered":"https:\/\/blog.pelleys.com\/?p=806"},"modified":"2022-05-30T10:27:23","modified_gmt":"2022-05-30T12:57:23","slug":"another-aide-memoire-renaming-files-based-on-modification-time","status":"publish","type":"post","link":"https:\/\/blog.pelleys.com\/?p=806","title":{"rendered":"Another Aide-m\u00e9moire &#8211; Renaming Files Based on Modification Time"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I have a bunch of weather station time lapse daily web cam files (found here: <a href=\"https:\/\/devweb.pelleys.com\/wxtl\">Historical 24-hour WX Timelapse Videos<\/a>) 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 &#8220;get around to&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I finally got &#8220;around to it&#8221; (after getting tired of experimenting with pfSense haproxy for now). So that I will remember how I did it, my code is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# Rename the file based on the modified date\n#\n# go through the list of .mp4 files\nfor file in *.mp4 ; do\n# Part 1 - for each file get the modification date and put it in a string. $file needs double quotes \n# due to spaces in the original\n        part1=\"$(stat -c %Y \"$file\")\"\n# 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  \n        part2=\"fixed\/$(echo \"$part1\" | awk '{ print strftime(\"%Y-%B-%d\", $1)\".mp4\" }')\"\n# Uncomment this to test first :-)\n#       echo $file \" is now\" $part2\n# This is the actual move of the original file (double quotes due to the spaces in the filename) to \n# the new directory. Use --preserve so the modification date doesn't change\n        cp --preserve \"$file\" $part2\n# All done\ndone<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It may not be the prettiest, most efficient code, but it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/blog.pelleys.com\/?p=806\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-806","post","type-post","status-publish","format-standard","hentry","category-things-i-would-like-to-remember","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=\/wp\/v2\/posts\/806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=806"}],"version-history":[{"count":0,"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=\/wp\/v2\/posts\/806\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pelleys.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}