Import photos – bash script

#!/bin/bash
#/usr/local/bin/auto-import-images
# Script auto-import images from folder(usb-attached camera)
camera_path=/media/camera
storage_path=/home/user/Pictures
regex='.*.(jpg|jpeg|bmp)'
date=`date +%Y`/`date +"%d. %B"`

#set display for messages
export DISPLAY=:0.0

# exit if I am already running
RUNNING=`ps --no-headers -C auto-import-images | wc -l`
if [ ${RUNNING} -gt 2 ]; then
  echo "Already running"
  exit 1
fi

# check if camera storage is mounted
mountpoint $camera_path > /dev/null
if [ "$?" == "0" ]; then
  images=`find $camera_path -iregex $regex | wc -l`
  if [ $images != 0 ]; then
    zenity --info --title 'Importing images' --text 'Importing images. Do not disconnect camera!'
    if [ ! -d "$storage_path$date" ]; then
      mkdir -p "$storage_path$date"; fi
    find $camera_path -iregex $regex | xargs -I file mv file "$storage_path$date"
    if [ "$?" != "0" ]; then
      zenity --info --title 'Import failed' --text 'Something went wrong, importing photos failed!';
    else
      sync
      umount $camera_path
      zenity --info --title 'Importing done' --text 'All photos where imported. You may disconnect the camera.'
      gnome-open "$storage_path$date"
    fi
  fi
fi

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>