Skip to content

Instantly share code, notes, and snippets.

@ncherro
Last active December 18, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncherro/5746111 to your computer and use it in GitHub Desktop.
Save ncherro/5746111 to your computer and use it in GitHub Desktop.
#!/bin/bash
SRC=~/Pictures
DST=~/Pictures-flat
# 1. Recursively finds all files ending in .jpg, .jpeg, .cr2 and .png in the
# $SRC directory (extension search is case-insensitive)
#
# 2. Gets each file's date created and creates a YYYY-MM-DD directory in the
# $DST directory
#
# 3. Copies the file into it's $DST/YYYY-MM-DD directory, preserving it's
# original created / modified dates and not overwriting existing files
while IFS= read -r -u3 -d $'\0' filename; do
# get the date created
fdate=$(stat -t %Y-%m-%d "$filename" | awk '{print $12}' | sed s/\"//g);
d="$DST/$fdate"
# create a date created directory
if [ ! -d "$d" ]; then
mkdir -v "$d"
fi
# copy
# -n do not overwrite existing file
# -p preserve created / modified dates
# -v verbose
cp -npv "$filename" "$d";
done 3< <(find $SRC -type f \( -iname '*.jpg' -o -iname '*jpeg' -o -iname '*.cr2' -o -iname '*.png' \) -print0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment