0

I'm new to Linux, and looking for help with a script. I'm using Resilio to back up photos from our iPhones. They go to '/media/data/phones/myiphone/resilio'

unfortunately resilio imports the photos in their iOS folders, each of which could contain up to 100 pictures per folder. As a folder fills up, blasted iOS makes a new random DCIM folder (100APPLE, etc), so folders will be added to the /resilio directory periodically as dictated by the iPhone. This is the part I'm finding tricky as I don't want to change the script each time a new DCIM folder is added.

I would like to extract any pictures and videos in each of these "iOS" folders and move them to a new single directory so all pics are in one folder (sorry, directory):

/media/data/phones/myiphone/pics

I would like for this to be done once a day.

(The pics directory will then be synced to my windows machine where I do my photo editing, etc. but that's not the part I need help with.)

If someone could walk me through this from beginning to end, including how to schedule the script to run, I would tremendously appreciate it. Pretend like you were explaining it to a kindergartner (who can read and type).

I'm happy to clarify anything that is unclear. Thx!!

1

First write a simple script to move the picture/videos to the desired directory. Something like the following, in a file named "backupPhotos.sh". I must assume that you know how to use a text editor.

#!/bin/bash

cd /media/data/phones/myiphone/resilio
find . -type f -exec mv {} ../pics \;

The first line says to run this script using /bin/bash (typically the default shell that you use in a terminal window). The line starting with "cd" changes the current working directory to the directory where your photos are saved by Resilio. The last line moves all the files under the resilio directory to the pics directory (only files will be moved "-type f", not the directory structure.

then do:

chmod 755 backupPhotos.sh
mkdir /media/data/phones/myiphone/pics

The chmod command tells Ubuntu that the file contains an executable script. The mkdir command creates the "pics" directory to hold the pictures/videos. If you already have the pics directory, then you don't need to do the mkdir.

Try out the script by typing the command

./backupPhotos.sh

in the directory where you created that file. Please take precautions to ensure that you have the pictures saved somewhere else during this testing phase, just in case something goes wrong. After you run the script, all the directories under "resilio" should be empty, and all the pictures/videos should be in the "pics" directory.

Once you are confident that this is working correctly, you can schedule it to run using crontab. Before doing the next command, you should identify your preferred text editor by typing

export EDITOR=gedit

This will set your preferred editor to "gedit" (temporarily). Replace "gedit" with any text editor that you like and have installed. Then type the command

crontab -e

This will put you in your favorite editor with a likely empty crontab file. There should be some text in the crontb file, but it should all be commented out with the "#" character. You will add a line at the end of the file to schedule running your new script. The line will be:

0 5 * * * /path_to_script_directory/backupPhotos.sh

where "path_to_script_directory" is the full path to where the script is located (something like /home/mkinsocal). The three asterisks mean run the script every day. The "0 5" means run it at 0 minutes after 5AM. Note that you must have the computer up and running at 5AM every day for this to work. Once you have finished adding the line to your crontab file, save it, and you are done.

0

I take it you have a folder structure like /media/data/phones/myiphone/pics/100APPLE,/media/data/phones/myiphone/pics/101APPLE and so on. This can be solved with globbing. Globs basically use special characters as wildcards.

  • * means any character, any number of times, including none.
  • ? means any character, at least once.
  • [abc] will match one character listed, appearing at least once.
  • [a-z] will match one character in the interval.

Thus ls [0-9]foo will match 1foo and 9foo, but not 12foo. *foo will match both.

Thus, the easiest glob may be ???APPLE, to match any folder with 8 characters in name, ending in APPLE, but with three unknown starting characters.

Matching any file inside can be done with *, e.g. ls ???APPLE/* will list all files in all those folders.

I've used ls as an example here, but globbing is handled by the shell, and you can equally well mv the files instead of listing them - or listing them, and running through a for loop.

Another approach would be using find, as @Drymartini suggests. In short, you have multiple possibilities.

  • but how will lsing the files help to move them? This certainly doesn't "walk [OP] through from beginning to end", does it? They say they are new to Linux and probably aren't familiar with the mv command... – Zanna Oct 17 '17 at 18:38
  • Much appreciated, but as Zanna mentioned, I'm way new to all this. i.e., have never written a script, shell, whatever. Very eager to learn, but possibly a little more guidance, or at least some terms that I can google. Vidarlo, you're correct about the file structure, although there is one random folder Group291. Only 1 photo there. Not sure why, but I'm fine ignoring it so long as it's not a trend. Still kinda lost though. :) – mkinsocal Oct 17 '17 at 19:41
  • I suggest you play around with listing files to understand globbing. Globbing happens in the shell, so mv *APPLE/* tmp/ will move all files of subdirectories ending in APPLE to the directory tmp. – vidarlo Oct 17 '17 at 19:47
-3

You can do this with man find

Something like this would help:

find ./ -iname *.jp?g -exec cp '{}' photosDir/ \;
  • You should quote any wildcards you want to pass to find, otherwise the shell will expand the expression to all matching files in the current directory, which will cause find not to find the things you want. And what about automation "I want this to happen once per day"? – Zanna Oct 17 '17 at 18:40
  • 1
    What's the goal of the ? in *.jp?g? When passed to -iname or -iname the ? character causes find to match exactly one character. *.jp?g does not match filenames like a.jpg. Note that this works the same as when the shell expands it (though you don't want that to happen here and @Zanna is correct that the pattern should be quoted). – Eliah Kagan Oct 25 '17 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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