Automounting SD card when inserted in Ubuntu

6,048

Solution 1

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)

Solution 2

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:
6,048

Related videos on Youtube

Khalid
Author by

Khalid

Updated on September 18, 2022

Comments

  • Khalid
    Khalid over 1 year

    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 :)

    • nerdwaller
      nerdwaller over 11 years
      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).
    • jam
      jam over 11 years
      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"
    • Khalid
      Khalid over 11 years
      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
      Khalid over 11 years
      I meant moving files and rename them according to their creation date and time :)
    • nerdwaller
      nerdwaller over 11 years
      @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.)
    • Khalid
      Khalid over 11 years
      Well I was thinking about creation time, but now that you have mentioned EXIF... :D
    • jam
      jam over 11 years
      @Khalid oops, misread sorry! Looks like others have it covered though :)
  • Khalid
    Khalid over 11 years
    Is it possible with EXIF data?
  • nerdwaller
    nerdwaller over 11 years
    This is a great source - I missed that one!
  • nerdwaller
    nerdwaller over 11 years
    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
    nerdwaller over 11 years
    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
  • Khalid
    Khalid over 11 years
    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.
  • nerdwaller
    nerdwaller over 11 years
    You must take loads of photos! You can definitely do that. I'll write a quick script when I get into work to try it out. You can post your udev rule if you want and we can try to help with that.
  • nerdwaller
    nerdwaller over 11 years
    I am currently updating my response for you, hopefully it get's accepted as doing what you want. No nested conditions necessary!
  • Khalid
    Khalid over 11 years
    Thanks a lot! :) Here is what I put as a udev rule 'ACTION=="add", DRIVERS=="sd", RUN+="/root/move_pictures_sd.sh"' then I restarted udev and inserted a memory card but the rule didn't work. I also tried adding vendor and model parameter but with no effect as well.
  • nerdwaller
    nerdwaller over 11 years
    I have a few thoughts (I am just starting from the base, so if you know this - please don't be offended :D). First - does your script have shebang (#!/bin/bash - assuming you use bash) up front and made executable (chmod +x /root/move_pictures_sd.sh)? Secondly, I think you will want more specific identifiers (probably Model and Vendor - which you can select more than one of each with comma separations I think - white space probably matters). Also, as the Ubuntu article suggests, name the rule something high like 92-name.rules To test it, I would try just a logging script.
  • Khalid
    Khalid over 11 years
    I double checked all of your suggestions. And that's the rule: ACTION=="add", SUBSYSTEMS=="scsi", DRIVERS=="sd", ATTRS{vendor}=="Generic ", ATTRS{model}=="Mini SD Reader ", RUN+="/root/move_pictures_sd.sh" Still didn't work. I made two changes to the script since it doesn't take subfolders in account. First is the SOURCE variable, I added DCIM folder in the path. Second is changing -maxdepth to 2 in find line. If there is more than one subfolder, I noticed that the script only scans the first one.
  • Khalid
    Khalid over 11 years
    Another thing that the system annoyingly randomly switches the path to the card between /dev/sdb1 and /dev/sdc1 whenever I put a card.
  • Khalid
    Khalid over 11 years
    Also it takes about 2 minutes to move a file! I think the cause is the identify statement. EDIT: Just checked it in top, identify statement is taking 94% of the CPU!
  • nerdwaller
    nerdwaller over 11 years
    Part of that could be that it is doing lots of reads and writes to the SD card and attempting to do so simultaneously. You might want to copy all images to a temporary working directory on the local disc before processing the images.
  • Khalid
    Khalid over 11 years
    Yes I was thinking about that too, and for the mounting point problem I will rely on the UUID of the drive instead which won't change, I suppose...
  • Khalid
    Khalid over 11 years
    Copied them to /tmp and it is still very slow :( I think that I will depend on the date written in the file name instead.
  • nerdwaller
    nerdwaller over 11 years
    @Khalid Cool, no worries. I used this script (slightly modified) the other day over all my photos (about 82g), it worked pretty well but wasn't the fastest. Perhaps I can figure that out sometime.
  • Khalid
    Khalid over 11 years
    It takes about two minutes to process one photo. What modifications should I make in order to let the script read the date in the filename and act on it?
  • Khalid
    Khalid over 11 years
    That did it. I am using several SD cards and each has its own UUID, I tried using ????-???? wildcard but it didn't work, any ideas? :)