Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to write a BASH script that automounts an SD card once it is inserted in the card reader, then move all pictures to a folder in the HDD and renaming each file to the date and time the picture was taken at.

Any ideas how to achieve that? I am using Ubuntu 12.04 with no GUI Thanks :)

share|improve this question
2  
I believe UBUNTU auto-mounts cards on plugin, at least my old install did. In any case, I think you will need to use a combination of UDEV rules specific to the UDID of your SD. That would be the advanced way, anyway. Your other option is a CRON job that executes your script every x-mins (obviously your script would need to check if the device exists or not and mount if so). –  nerdwaller Nov 15 '12 at 16:17
1  
udev is a nice easy way of doing this. Do be aware some old guides refer to old ways of running scripts. I believe the new way is RUN+="/path/to/script" –  jam Nov 15 '12 at 16:29
    
Sorry forgot to mention that there is no GUI. I will give udev rules a try. What about renaming? Is there any trick to do that? Thanks for your comments :) –  Khalid Nov 15 '12 at 16:35
    
I meant moving files and rename them according to their creation date and time :) –  Khalid Nov 15 '12 at 17:01
    
@Khalid Do you want creation time or exif time? EXIF is the meta data in an image that records data about the cam that took the picture (Time, Date, Aperture, Make, Model, F-Stop, etc.) –  nerdwaller Nov 15 '12 at 17:03
show 2 more comments

2 Answers

up vote 1 down vote accepted

Just to provide a full answer for you:

Auto mount and copy over data

Advanced Way (how you should do it)

You will want to use UDEV rules that are specific to the UUID of your device (pardon my above type of udid).

Lazy Way (how you could do it)

Run a CRON Job to execute your script at a specified interval. You would obviously need the script to verify that the SD is plugged in and/or mounted before executing the rest).

Rename / Move

To get EXIF data

$ identify -verbose imageFile.jpg

That spits out loads of goodies, so let's suppose you want the creation date from that - let's filter that down:

$ identify -verbose imageFile.jpg | awk '/exif:/' (or grep exif)


Update:

OP requested another method in the comments:

You are asking to nest folders instead, for example November 20, 2012 would be ~/2012/11/20/*.jpg's

Here is the script I made to try this, it works with EXIF now (the initial items for you were based on creation date, which would be floating as you move or copy the file). It's hosted on my blank site for you and I will post a screenshot too (since it's a lot of effort to make it formatted here). imageByEXIF.sh

imageByEXIF.sh

I would recommend you test this on a limited scale before implementing it. I put in Command Line Args for testing, so I was doing ~/Pictures to ~/Documents (although it prefers the full path)

share|improve this answer
    
Is it possible with EXIF data? –  Khalid Nov 15 '12 at 17:04
    
@Khalid - You are on Linux, of course it is! identify -verbose "INPUT" | grep "exif:DateTime:" | awk -F\ '{print $2" "$3}' –  nerdwaller Nov 15 '12 at 17:15
1  
Edit: This should actually work, just tested identify -verbose inputFile.jpg | grep "date:create:" | awk '{print $2}' | awk -FT '{print "date: "$1" time: "$2}' I will leave it up to you to integrate that to your script, if you need help - let me know what you tried and I will try to help :D. (if you want other data, just leave it at identify -verbose image.jpg, there is some cool stuff. –  nerdwaller Nov 15 '12 at 17:29
    
I realized how much extra crap I have in the above, so I am shortening it for you: identify -verbose imageFile.jpg | awk '/date:create:/ {split($2,arr,"T"); print arr[1],arr[2]}' That is much prettier, arr[1] is the date and arr[2] is the time –  nerdwaller Nov 15 '12 at 21:08
    
Thanks for the script :) I tried using udev rules but it didn't work out for some reason... so I am going the cronjob way. I had another idea in my mind, is it possible to make the script put a photo in a folder according to its EXIF date? for example if a photo is taken today, it will be in 20 subfolder inside 11 subfolder inside 2012 subfolder. I guess I'll need to use nested if conditions for that. –  Khalid Nov 20 '12 at 11:54
show 14 more comments

Here lies your answer to mighty mounting trigger in ubuntu: https://help.ubuntu.com/community/UsbDriveDoSomethingHowto

Just type a script to move photos. Like:

#!/bin/bash
#
# destination file like ../20121115-174656-originalname

source=/mnt/sdcard/
destination=/home/youraccount/foto/

cd $source

for i in `find . -maxdepth 1 -type f -printf "%f\n"`; do

iinfo=`stat -c%y $i | cut -c1-19`
idate=`date --date="$iinfo" +%Y%m%d-%H%M%S-`

echo mv $i $destination$idate$i

done
share|improve this answer
    
This is a great source - I missed that one! –  nerdwaller Nov 15 '12 at 17:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.