Last active
March 2, 2017 15:35
-
-
Save davidalger/da422fee1f3cc4730e309be8f3209938 to your computer and use it in GitHub Desktop.
This script will handily sort millions of mail files out of a mail inbox into a series of directories organized by month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
maildir="$1" | |
if [[ "$maildir" == "" ]]; then | |
echo "Usage: mail-sort.sh <maildir> <dest>" | |
exit -1 | |
fi | |
if [[ ! -d "$maildir" ]]; then | |
echo "Please make sure '$maildir' is a directory!" | |
exit -1 | |
fi | |
destdir="$2" | |
if [[ "$destdir" == "" ]]; then | |
echo "Usage: mail-sort.sh <maildir> <dest>" | |
exit -1 | |
fi | |
if [[ ! -d "$destdir" ]]; then | |
echo "Please make sure '$destdir' is a directory!" | |
exit -1 | |
fi | |
# loop over all files in the directory; since there could be millions of files, disable sort and stat for performance | |
for filename in $(ls -U1 $maildir); do | |
# first part of name contains unix timestamp, so grab that and format into yy-mm format | |
unixtime="$(echo "$filename" | cut -d. -f1)" | |
filesort="$(date -d @$unixtime +%Y-%m)" | |
# create directory here (or comment out and manually create them for minor performance boost) | |
# mkdir -p "$destdir/$filesort" | |
# relocate file into the proper sort directory | |
mv "$maildir/$filename" "$destdir/$filesort/$filename" | |
# print new file path | |
echo "$destdir/$filesort/$filename" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment