Script Bash: copy new images from SD to PC

Sometimes we need to perform repetitive tasks on our PC, which over time become tedious. In certain cases we can ease our work by making use of scripts that work for us.

Today I am writing to show you a bash script that solved a need: copy new images from my digital camera from an SD card to the PC.

Location hotel:

Every time I had new images to download to my computer, I would do the following:

1. Open the directory where I have the images classified in sub-directories.

2. Create a new sub-directory with the name of the current date, in format

3. Move to the sub-directory created the previous time and see what is the last image saved.

4. Copy the new images from the SD card to the newly created directory.

This takes no more than a minute, but it is easier for the process to be done by just inserting the card.

I have to use the card because I cannot directly transfer the images from the camera via USB, since there are no drivers for Linux (as always happens…)


Create a bash script that does the following:

1. Verify at startup if the SD card is mounted. Otherwise it ends.

2. Go to the main directory of images and find the last one. Save your name in a variable.

3. Compare the current date with the last directory, if they differ, create a new directory with the name of the current date in the format "".

4. Move to the last directory (not the new one, but an older one) and save in a variable the name of the last file transferred the previous time.

In this step it is necessary to filter the name of the file so that it can be compared with the new files on the card in the near future. The files have the following format: xxx_xxxx.eee Where: x = digit 0 to 9 and eee = extension (JPG, MOV). For example: 100_5684.JPG, 100_5699.MOV. After the filter, the name remains xxxxxxx Therefore, in the example above, we would have: 1005684, 1005699.

Since the directory can contain other types of files or with altered names, the filter is used.

5. Move to the card and filter the files as in the previous point.

6. Compare the files on the card with the variable that contains the last file transferred the previous time (point 4) and copy the named files into the newly created directory greater than the variable. (since names are just numbers).

7. Open the directory containing the new images with the file manager.

Next I show you the script with comments that explain how it works. I clarify that I am not a programmer and it gave me several headaches until I got it to work, especially when I had to filter the names for the "for" loop.

#! / bin / bash ### --- VERIFY IF SD IS MOUNTED --- ### SD = / media / KODAK / DCIM / 100Z8612 if [[-d $ SD]]; then ### --- CREATE DIRECTORY --- ### #Read directory of images and create another with the name of the #current date and permissions 755 if it does not exist. cd ~ / Pictures / kodak ULTDIR = `ls -1 | tail -n1` # last directory in the list. DATE = `date +% y.% M.% D` #Current date in YY.MM.DD format if [" $ LASTDIR "! =" $ DATE "]; then mkdir -vm 755`date +% y.% m.% d` # create directory with current date fi ### --- SEE LAST FILE OF $ ULTDIR --- ### cd $ ULTDIR ULTIMG = `ls - 1 [0-9] [0-9] [0-9] _ [0-9] [0-9] [0-9] [0-9]. [JM] [PO] [GV] | tail -n1 | cut -c1-3,5-8` # see the last image with name xxx_XXXX.eee .eee = file extension (JPG or MOV) # To ensure that the script works after it is fulfilled: # 100_9999.eee - > 101_0000.eee and there are no errors # CUT so that it is in the format xxxXXXX ### --- MOVE TO THE LAST DIRECTORY OF THE LIST --- ### # OR THE RECENTLY CREATED, IF # cd was CREATED .. ULTDIR = `ls -1 | tail -n1` # goes again because otherwise it takes the previous ULTDIR of the if cd / media / KODAK / DCIM / 100Z8612 ### --- FILTER the files in SD --- ### FILTER = `ls -1 [0 -9] [0-9] [0-9] _ [0-9] [0-9] [0-9] [0-9]. [JM] [PO] [GV] `### - - COMPARE NEW FILES WITH NAME GREATER THAN LAST --- ### for I in $ FILTER do N = `echo $ I | cut -c1-3,5-8` #Cut name if [["$ ULTIMG" -lt "$ N"]]; then cp $ I ~ / Pictures / kodak / $ ULTDIR fi done thunar ~ / Pictures / kodak / $ ULTDIR #Open the new directory with Thunar else exit 0 fi exit 0

Finally to get it working, I added it to Xfce's "Removable Drives and Media" application in the menu

Settings → Xfce 4 Settings Manager → Removable Drives and Media → Cameras

using the import image option. When I insert the card, a dialog box asks if I want to import the images. On accepting, the script is executed.

Well that's it. Sorry for the messiness of the text, it's my first post and I don't know how to tabulate the script when editing it. I hope it is useful to someone at least to have an idea and adapt it to each particular case.

I did not license because I have not informed myself well about each version of the GPL, but they are at liberty to copy and modify it. And feel free to say how it could be improved or if there are other ways to do the same, but easier.

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

13 comments, leave yours

Leave a Comment

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



  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Percaff_TI99 said

    Very interesting Joaquín, a good idea is to upload the script in the paste where it is easier to share the code, and as you say it can be adapted to other needs and one learns to use bash a little more.


    1.    Joaquin said

      Done, added in
      Thank you!

  2.   vr_rv said

    When copying a photograph that is already in the directory, it does not indicate that there is already one and gives you the option of replacing it or not copying it?

    Anyway it is very useful, since it does everything automatically

    1.    Joaquin said

      Hi. It doesn't actually copy repeating images, it just ignores them. The idea was precisely to copy the new files without the need for user interaction. Also, unless you add images more than once a day, the script copies the new files to a new directory. I explain briefly:

      Today is 10/03/13, I create a directory called 13.03.10/100/4440 and copy some images or video (MOV) inside: 100_4441.JPG, 100_4442.JPG, 100_4445.MOV, XNUMX_XNUMX.JPG
      (The missing 4443 and 4444 were erased with the camera, the last one is 4445).

      Then suppose I use the script on 01/04/13 and have new images on the card. The script creates a directory called 13.04.01 and inside the new images / videos whose name is greater than "4445"; because it reads the directory 13.03.10 and the last image is 100_4445.JPG. If I rerun the script with new card images that same day, they are added to the 13.04.01 directory. None are overwritten.

      I hope I have clarified my idea a bit 🙂

  3.   Manuti said

    And you do not know Rapid Photo Downloader? I think it does all of that and more.

    1.    Mr Black said

      The thing is that the skinny guy did it himself, he would have had fun I suppose and it can serve someone else

    2.    Joaquin said

      Hi. I did not know it and it seems good from what I saw on the internet, but actually the script perfectly fulfills what I need; which is to insert the card and have all the files copied automatically.

      It also helped me to learn a little more because obviously it didn't work the first time; Mistakes occurred and you learn a lot from them.

      I did not think to show the script to solve the problems to others, there are applications like the ones you mention that are more general. I shared it for you to read and as a reference to create new things making use of loops (in this case "for") and regular expressions (variables "ULTIMG" and "FILTER" in the script)

  4.   codelab said

    It seems useful, I keep it in case I need it one day. Thank you.

    1.    Joaquin said

      Thank you!

  5.   nonamed said

    but for that we already have grsync

    1.    Joaquin said

      Yes, but I think rsync is used more for backups. I have never looked at it but it is an application that we should take into account and learn from all of us.

      I didn't know there was a graphical interface for rsync.

  6.   lastnewbie said

    Wow, I find the code very interesting, very useful. keep it up. 😀
    I'll keep it in case I need it.


    1.    Joaquin said

      Thank you!.