Skip to content

Instantly share code, notes, and snippets.

@larruda
Created July 25, 2013 19:22
Show Gist options
  • Save larruda/6082889 to your computer and use it in GitHub Desktop.
Save larruda/6082889 to your computer and use it in GitHub Desktop.
Shell script used to organize files by date in a directory tree. Tested and used on Mac OS X.
#!/bin/bash
function navigateRecursively {
currentDirectory=$1
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
BASEPATH=$2
i=0
#echo $currentDirectory
for d in `find $currentDirectory -maxdepth 1 | tail -n +2`;
do
i=`expr $i + 1`
if [[ "$d" == "$BASEPATH" ]]; then
continue
fi
if [ -d $d ]; then
navigateRecursively $d $BASEPATH
fi
createdTimestamp=`stat -f '%B' $d`
date=`date -r $createdTimestamp "+%Y %B %d %m"`
declare -a DATE_ARRAY
DATE_ARRAY=(`tr " " "\n" <<< $date`)
destination="${BASEPATH}/${DATE_ARRAY[0]}/${DATE_ARRAY[3]} ${DATE_ARRAY[1]}/${DATE_ARRAY[2]}/"
echo "FROM: $d"
echo -e "DEST: $destination\n"
mkdir -p $destination
mv "$d" $destination
if [ $? -ne 0 ]; then
echo "OVER"
exit 1
fi
done
IFS=$SAVEIFS
}
navigateRecursively $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment