Skip to content

Instantly share code, notes, and snippets.

@pacohope
Created September 20, 2018 08:34
Show Gist options
  • Save pacohope/992d93900ac5e4d4acd6d3e22f11177f to your computer and use it in GitHub Desktop.
Save pacohope/992d93900ac5e4d4acd6d3e22f11177f to your computer and use it in GitHub Desktop.
Sort all JPG files into subfolders based on modification date/time
#!/bin/bash
#
# Take a bunch of files, figure out the modification dates using stat(1), make a
# bunch of directories that correspond to YEAR/MONTH and then move all the files
# into those directories. E.g., FOO.JPG gets moved to 2018/05/FOO.JPG if its
# modification date is 2018-05-14
# This uses the MacOS syntax for stat(1)
# First, figure out and make all the necessary directories
stat -t "%Y/%m" *JPG | cut -d \" -f 8 | sort -u | xargs mkdir -p
# For each file, figure out what directory it needs to go in. Put it there.
for i in *JPG
do
dir=$(stat -t "%Y/%m" ${i} | cut -d \" -f 8 )
mv "${i}" "${dir}"
done
# Copyright (c) 2018 Paco Hope
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment