{"id":65,"date":"2016-02-10T17:43:54","date_gmt":"2016-02-10T16:43:54","guid":{"rendered":"http:\/\/mickael.bucas.name\/blog\/?p=65"},"modified":"2019-01-31T12:37:31","modified_gmt":"2019-01-31T11:37:31","slug":"sorting-files-by-date","status":"publish","type":"post","link":"https:\/\/mickael.bucas.name\/blog\/2016\/02\/sorting-files-by-date\/","title":{"rendered":"Sorting files by date"},"content":{"rendered":"<p>My phone saves pictures with names in the format<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">IMG_YYYYMMDD_HHMMSS.jpg<\/pre>\n<p>I upload all images to my machine in a single directory, but I want to sort them by date. I use the following commands to sort them, in two phases :<\/p>\n<p><strong>First, create the directories<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ls|cut -d_ -f2|sort -u|while read date; do\nmkdir -p \/data\/pictures\/${date:0:4}\/${date:0:4}-${date:4:2}-${date:6:2};\ndone<\/pre>\n<ul>\n<li><code>ls<\/code> gives all image names<\/li>\n<li><code>cut -d_ -f2<\/code> extracts the date of each picture<\/li>\n<li><code>sort -u<\/code> removes duplicate dates with the -u (unique) option<\/li>\n<li><code>while read date<\/code> loops on all dates to run the next command<\/li>\n<li><code>mkdir -p<\/code> creates the directory for the date, and eventually the year<\/li>\n<li>The syntax <code>${date:0:4}<\/code> extract parts of the date. In this case the year<\/li>\n<\/ul>\n<p><strong>Then move images into directories<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">ls|cut -d_ -f2|sort -u|while read date; do\nmv *$date* \/data\/pictures\/${date:0:4}\/${date:0:4}-${date:4:2}-${date:6:2};\ndone<\/pre>\n<p>Only the command inside the loop is different :<\/p>\n<ul>\n<li><code>mv *$date*<\/code> moves all pictures of the same day in the target directory<\/li>\n<\/ul>\n<p>If the date is not in the file name, but is available in the directory, you can get it with the command <code>ls --full-time<\/code> and parse the output, or you can use the command <code>stat<\/code> to get exactly what you want, as it gives you full control on the output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My phone saves pictures with names in the format IMG_YYYYMMDD_HHMMSS.jpg I upload all images to my machine in a single directory, but I want to sort them by date. I use the following commands to sort them, in two phases : First, create the directories ls|cut -d_ -f2|sort -u|while read date; do mkdir -p \/data\/pictures\/${date:0:4}\/${date:0:4}-${date:4:2}-${date:6:2};&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,8],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux"],"_links":{"self":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":9,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/posts\/65\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mickael.bucas.name\/blog\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}