Skip to content

Instantly share code, notes, and snippets.

@hugot
Last active April 29, 2017 21:58
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 hugot/41d8322d063067fbc6f86f7e8d7ffb23 to your computer and use it in GitHub Desktop.
Save hugot/41d8322d063067fbc6f86f7e8d7ffb23 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# image_sorter.sh
# Copyright (C) 2017 Hugo
#
# Distributed under terms of the MIT license.
#
# This script will sort the image and video files from an android phone by date by placing them in directories
# according to the dates that can be extracted from the filename. The filenames of the images must be structured
# as follows: yyyymmdd_HHMMSS.jpg or yyyymmdd_HHMMSS.mp4 (other extentions can be added easily)
#
#
# Sort the files by folder, input is as follows:
# input 0 = the original filename, input 1 = the directory the file will be in
# input 2 = the new filename
sortFiles(){
while read -a input
do
if ! test -d ${input[1]}
then mkdir -p ${input[1]}
fi
mv ${input[0]} ${input[1]}/${input[2]}
done
}
# Test the input from pipe (just switch it with sortFiles at the end of the pipe)
testIt(){
while read -a input
do
echo ${input[@]} zero : ${input[0]} one: ${input[0]} tho ${input[1]} three: ${input[1]}
if ! test -d ${input[1]}
then echo making dir ${input[1]}
mkdir -p ${input[1]}
fi
echo copying ${input[0]} ${input[1]}/${input[2]}
done
}
ls | grep '.*\.jpg\|.*\.mp4' | sed -r -e 's/((^[0-9]{4})([0-9]{2})([0-9]{2})_([0-9]{2})([0-9]{2})([0-9]{2})(.*\..*$))/\1 \2\/\3-\4 \3-\4-\5:\6:\7\8/g' | sortFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment