Skip to content

Instantly share code, notes, and snippets.

@MagnusEnger
Created October 23, 2011 19:15
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 MagnusEnger/1307746 to your computer and use it in GitHub Desktop.
Save MagnusEnger/1307746 to your computer and use it in GitHub Desktop.
Sort pictures into year/month/date folders based on the filename yyyymmdd_x.jpg
#!/bin/bash
# Read a directory of image files with names on this form:
# 20110113_001.jpg
# 20110113_002.jpg
# 20110113_003.jpg
#
# Create folders as needed and move the images into them, in this way
# 2011
# |__01
# |__13
# |__20110113_001.jpg
# |__20110113_002.jpg
# |__20110113_003.jpg
#
# Usage: phosort.sh ~/Pictures
cd $1
for f in *
do
echo $f
# Construct the direcory path
dir="${f:0:4}/${f:4:2}/${f:6:2}"
# Check if the path exists
if [ ! -e $dir ]
then
echo "$dir does not exist"
# Create the directory if we have to
mkdir -p "$dir"
fi
# Move the file
mv $f $dir/$f
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment