Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Script to move files to new directory periodically

Writer Mia Lopez

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!!

3 Answers

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.

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.

3

You can do this with man find

Something like this would help:

find ./ -iname *.jp?g -exec cp '{}' photosDir/ \;
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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