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.
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 yy.mm.dd 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.
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 "yy.mm.dd".
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
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.