ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to create a bash script to take photos from a directory, rename and sort them based on the date found in the exif data made by my camera.
When the script is finished it should move all the files into this structure: ./YYYY-MM/YYMMDD-HHMMSS.ext
The section I am having trouble with is sorting the renamed files into folders.
This command
Code:
jhead -autorot -ft -nf%y%m%d-%H%M%S *
renames the files in the current folder to YYMMDD-HHMMSS.jpg taken from exif data.
I am new to scripting as I'm sure you can see, is there an easy way to strip the first four numbers from the file names, and sort them based on that?
Code:
#!/bin/sh
#
# move files from camera to sorting area
mv /<pathtomemcard>/* /<pathtosortlocation>/
# rename all image files in sorting area based on exif dates
# YYMMDD-HHMMSS.ext
jhead -autorot -ft -nf%y%m%d-%H%M%S /<pathtosortlocation>/*
# Sort files into folders based on the file name or exif date (whichever is easiest)
# NEED TO DO
# move sorted files into photo library
mv /<pathtosortlocation>/* /<pathtophotolib>/
Thanks in advance for any help!
Last edited by SuperJediWombat!; 04-30-2009 at 06:40 PM.
ls <pathtosortlocation> | while read f; do
# extract first 4 characters from filename
YYMM=${f:0:4}
# create directory if it does not exist
test -d $YYMM || mkdir $YYMM
# move the current file to the destination dir
mv <pathtosortlocation>/$f $YYMM/$f
done
#!/bin/bash
#
#
CameraPath="/home/angus/st/camera"
SortLocation="/home/angus/st/sort"
PhotoLibrary="/home/angus/st/library"
CharFromName=4
############
# move files from camera to $SortLocation
mv $CameraPath/* $SortLocation/
############
# rename all image files in $SortLocation
# YYMMDD-HHMMSS.ext
jhead -autorot -ft -nf%y%m%d-%H%M%S $SortLocation/*
###########
# Sort files into folders using $CharFromName letters of the file name
#
ls $SortLocation | while read file; do
# extract first $CharFromName characters from filename
YYMM=${file:0:$CharFromName}
# create directory if it does not exist
test -d $SortLocation/$YYMM || mkdir $SortLocation/$YYMM
# move the current file to the destination dir
mv -v $SortLocation/$file $SortLocation/$YYMM/$file
done
##########
# move sorted files into photo library
mv -v $SortLocation/* $PhotoLibrary/
would it be possible to set $CameraPath to either the string set in the script, or the first argument used when running it. ($1)?
So I could execute the script as ./ImageSort.sh /new/location/of/images
to use /new/location/of/images as the camera path, but if I don't specify an argument it just uses the value given inside?
Also, can I have it pause when run, before moving the files to the sort location, and echo $CameraPath for confirmation?
Thanks jan, it's all coming together. I've just found that when using mv to move a directory, it does not merge the folders if the target directory has files inside.
#!/bin/bash
#
#
PhotosPath="/media/4GBSD/DCIM/101CANON"
SortPath="/home/angus/.imagesort"
LibraryPath="/home/angus/Photos"
CameraPath="/media/4GBSD"
CharFromName=4
echo
echo
############
# Test to see if $PhotosPath exists, if not promp for new path / exit.
test -d $PhotosPath || read -p "$PhotosPath does not exist, close to exit or type new path:" PhotosPath
test -d $PhotosPath || "read -p '$PhotosPath is invalid. Press enter to close' && exit"
############
# move files from camera to $SortPath
mv $PhotosPath/* $SortPath/
############
# rename all image files in $SortPath
# FolderDateDD-HHMMSS.ext
jhead -autorot -ft -nf%y%m%d-%H%M%S $SortPath/*
###########
# Sort files into folders using $CharFromName letters of the file name
#
ls $SortPath | while read file; do
# extract first $CharFromName characters from filename
FolderDate=${file:0:$CharFromName}
# create directory if it does not exist
test -d $LibraryPath/$FolderDate || mkdir $LibraryPath/$FolderDate
# move the current file to the destination dir
mv -v $SortPath/$file $LibraryPath/$FolderDate/$file
done
##########
# move sorted files into photo library
#mv -v $SortPath/* $LibraryPath/
##########
# Umount the card
umount $CameraPath
##########
# End notification
echo
echo "Photos from: $PhotosPath"
echo "End location: $LibraryPath"
echo
echo "The card has been ejected."
echo
read -p "Press enter to close this window…"
A lot of effort gone into this script but it also also useful to note that jhead as used here can be used to move the photos directly into directories as it renames them by modifying the output string format:
e.g.:
jhead -n%Y/%m/%d/%f *.jpg
will put the photos into a directory structure of /year/month/day (I used %f to retain the original filename and there are differences between -n and -nf so check the manual).
(Also works in "the other os" but you need to use "\" not "/" for directories)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.