Skip to content

Instantly share code, notes, and snippets.

@dmpop
Last active January 16, 2024 00:51
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dmpop/9c2ad06019256df3ff0a6cecab3f3959 to your computer and use it in GitHub Desktop.
Save dmpop/9c2ad06019256df3ff0a6cecab3f3959 to your computer and use it in GitHub Desktop.
Photo import Bash shell script
#!/bin/bash
if [ -z "$(command -v notify-send)" ]; then
echo "Please install notify-send."
exit 1
fi
ext1=$3
ext2=$4
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
notify-send "Please specify the required parameters" -i important
exit 1
fi
# lsblk | grep 'mmcblk0p1' | cut -d' ' -f13
source_dir="$1"
target_dir="$2"
if [ ! -f "$source_dir/imported.txt" ]; then
touch "$source_dir/imported.txt"
fi
if [ ! -d "$target_dir" ]; then
mkdir -p "$target_dir"
fi
cd "$source_dir"
find . -type f -name "*.$ext1" | xargs -I{} rsync -avh {} --exclude-from "$source_dir/imported.txt" "$target_dir"
find . -type f -name "*.$ext1" | xargs -I{} basename {} >> imported.txt
if [ ! -z $ext2 ]; then
find . -type f -name "*.$ext2" | xargs -I{} rsync -avh {} --exclude-from "$source_dir/imported.txt" "$target_dir"
find . -type f -name "*.$ext2" | xargs -I{} basename {} >> imported.txt
fi
sort "$source_dir/imported.txt" | uniq >> "$source_dir/imported-sorted.txt"
mv "$source_dir/imported-sorted.txt" "$source_dir/imported.txt"
cd "$target_dir"
exiftool -d %Y%m%d-%H%M%S.%%e "-FileName<DateTimeOriginal" .
exiftool '-Directory<CreateDate' -d ./%Y-%m-%d -r .
notify-send "Import completed." -i info
@mabachel
Copy link

I added some commentary / documentation to make it easier to use for newcomers. In addition I made the script suitable for Continuous Shooting (multiple photos per second to select the best one afterwards). Feel free to add it to your own code.

#!/bin/bash

### Import Photos Bash Script by Dmitri Popov ###
# This custom Bash shell script transfers photos and RAW files, renames them in
# the yyyyMMdd-mmhhss format, and puts them in a separate directory named by the
# current date. The script is faster than digiKam’s import tool, and it allows
# you to quickly offload files from a storage card without starting digiKam.
#
# This script expects at 3-4 arguments:
# 1: Source Directory e.g. /run/media/username/disk/DCIM/100MSDCF
# 2: Target Directory e.g. ~/Pictures/holiday
# 3: Filename Extension e.g. JPG
# 4 (optional): 2nd Filename Extension e.g. ARW
#
# Note on Directory Paths: Directory paths must not have a slash at the end an
# must not be local but global (e.g. use ~/Pictures/holiday instead of holiday
# while being in ~/Pictures).
#
# Note on Continuous Shooting: If you import photos taken with Continuous
# Shooting (multiple photos per second to select the best one afterwards) will 
# be renamed in yyyyMMdd-mmhhss-c format where c is a ascending number.
#
# Created by Dmitri Popov https://gist.github.com/dmpop
# Comments are based on Revision 17 as of Sep 10, 2016.
# 
### Commentary added by mabachel https://github.com/mabachel on Mar 24, 2019 ###

if [ -z "$(command -v notify-send)" ]; then
  echo "Please install notify-send."
  exit 1
fi
ext1=$3
ext2=$4

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
  notify-send "Please specify the required parameters" -i important
  exit 1
fi
# lsblk | grep 'mmcblk0p1' | cut -d' ' -f13
source_dir="$1"
target_dir="$2"
if [ ! -f "$source_dir/imported.txt" ]; then
    touch "$source_dir/imported.txt"
fi
if [ ! -d "$target_dir" ]; then
  mkdir -p "$target_dir"
fi
cd "$source_dir"
find . -type f -name "*.$ext1" | xargs -I{} rsync -avh {} --exclude-from "$source_dir/imported.txt" "$target_dir"
find . -type f -name "*.$ext1" | xargs -I{} basename {} >> imported.txt
if [ ! -z $ext2 ]; then
  find . -type f -name "*.$ext2" | xargs -I{} rsync -avh {} --exclude-from "$source_dir/imported.txt" "$target_dir"
  find . -type f -name "*.$ext2" | xargs -I{} basename {} >> imported.txt
fi

sort "$source_dir/imported.txt" | uniq >> "$source_dir/imported-sorted.txt"
mv "$source_dir/imported-sorted.txt" "$source_dir/imported.txt"

cd "$target_dir"
# exiftool -d %Y%m%d-%H%M%S.%%e "-FileName<DateTimeOriginal" .
### replace above command with the following (by mabachel)
exiftool -d %Y%m%d-%H%M%S%%-c.%%le "-FileName<DateTimeOriginal" .
for file in $(find -name "*-*-1.*"); do
    OLD_NAME=$(echo $file | sed 's/\-1\././')
    NEW_NAME=$(echo $file | sed 's/\-1\./\-0\./')
    mv $OLD_NAME $NEW_NAME
done
### end replacement
exiftool '-Directory<CreateDate' -d ./%Y-%m-%d -r .
notify-send "Import completed." -i info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment