Skip to content

Instantly share code, notes, and snippets.

@stekan
Created August 7, 2017 18:04
Show Gist options
  • Save stekan/afadc9b97a297b6f879d09627e3e5a7e to your computer and use it in GitHub Desktop.
Save stekan/afadc9b97a297b6f879d09627e3e5a7e to your computer and use it in GitHub Desktop.
Copy emails to directory year/month/day
#!/bin/bash
SRC=/path/to/eml/src
DEST=/path/to/eml/dest
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
cd "$SRC"
for file in $(find -type f -name "*.eml")
do
EMAILDATE=$(grep -m 1 "^Date: " < "$file" | sed -e "s/Date://g")
y=$(date --date="$EMAILDATE" +%Y)
m=$(date --date="$EMAILDATE" +%m)
d=$(date --date="$EMAILDATE" +%d)
[ -d "$DEST/$y/$m/$d" ] || mkdir -p "$DEST/$y/$m/$d"
cp "$file" "$DEST/$y/$m/$d/"
done
IFS=$SAVEIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment