Search code examples
shelldatemovedirectorybulk

Shell script to move photos into folders with date YYYYMMDD


I have an IP camera that sends a photo every minute to a folder on a PC (Linux) with (mv *20150501* 20150501).

Where the camera sends the following filename:

Schedule_20150501-103642.jpg

I would like to make a shell script that:

  • Looks into the file name for the date in the format YYYYMMDD. Create a folder with that number if it doesn't exist and move all files containing this number into this folder.
  • Checks in each folder if there is an .MP4 file. If not, execute this script that produces a time-lapse video from the images:
mencoder "mf://*.jpg" -mf fps=12:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vbitrate=7000 -oac copy -o Zeitraffer`**20150501**.mp4

Base folder is: snap

Sub folder: snap/(date +%YYYY%mm%dd)

Until now, I do all that manually and separately using mv. Then I move into the folders and modify the script for the time lapse. :-(

Until now, I have a script to make at least a bulk of folders by date:

#!/bin/bash

read -p "Geben Sie den ersten Ordner an (JJJJMMTT): " now  
read -p "Geben Sie den letzten Ordner an (JJJJMMTT): " end  

while [ "$now" != "$end" ] ;
do
now=`date +"%Y%m%d" -d "$now +1 day"`;
mkdir $now
done 

Any idea?


Solution

  • First of all, Thanks to every comment of you!!! REGEX is toooo difficult for me. Thanks alyway. After a lot of research today I fit together a lot of puzzle parts. What I got is this: The only thing is that if there is already a folder the respective files are not copied due to the while function. But as the script stops one day before today this should not be a problem. Then I made two more scripts for the case in the past I forgot to make a movie it will be created. The other checks if the MP4 file already exists in the plex folder and if not copy the file there. Actually I just modified the script for that.

    The script for "normal" work:

    #!/bin/bash
    read -p "Geben Sie den ersten Ordner an (JJJJMMTT): " begin  
    end=$(date +%Y%m%d -d "-1 day")                                       
     while [ $begin != $end ] 
    do
    begin=`date +"%Y%m%d" -d "$begin +1 day"`
    if [ ! -d $begin ]  # Prüfen, ob es den Ordner gibt.                                      
    then
        /bin/mkdir $begin 
        echo "Ordner $begin erstellt"
        mv *$begin*.jpg $begin  
    echo "Bilder vom $begin verschoben nach $begin"
     cd $begin  
     $(mencoder mf://*.jpg -mf fps=12:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vbitrate=7000 -oac copy -o Zeitraffer$begin.mp4)
      cp Zeitraffer$begin.mp4 /Plexfolder/ 
     echo "Kopiere Zeitraffer$begin.mp4 nach /RAID5/filme/Kamera/"
     cd ..
     fi
     done 
    

    The script to check if the MP4 file exists if not creates it and copies to the plex folder:

    #!/bin/bash
    
      read -p "Geben Sie den ersten Ordner an (JJJJMMTT): " begin  
      end=$(date +%Y%m%d -d "-1 day")       
      while [ $begin != $end ] 
      do
      begin=`date +"%Y%m%d" -d "$begin +1 day"`
      if [ -d $begin ] 
      then
      cd $begin 
      if [ ! -f Zeitraffer$begin.mp4 ] #Prüfen, ob es eine MP4 Datei gibt 
      then
      $(mencoder mf://*.jpg -mf fps=12:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vbitrate=7000 -oac copy -o Zeitraffer$begin.mp4)
      cp Zeitraffer$begin.mp4 /plexfolder/ 
      echo "Kopiere Zeitraffer$begin.mp4 nach /plexfolder/"
      fi  
      cd ..
      fi
      done 
    

    And the one to copy the missing MP4 file to the plex folder:

    #!/bin/bash
    read -p "Geben Sie den ersten Ordner an (JJJJMMTT): " begin  
    end=$(date +%Y%m%d -d "-1 day") 
    while [ $begin != $end ] 
    do
    begin=`date +"%Y%m%d" -d "$begin +1 day"`
    if [ -d $begin ]          
    then
    cd $begin 
    if [ ! -f "/plexfolder/Zeitraffer$begin.mp4" ] 
    then
    cp Zeitraffer$begin.mp4 /RAID5/filme/Kamera/
    echo "Kopiere Zeitraffer$begin.mp4 nach /RAID5/filme/Kamera/"
    fi  
    cd ..
    fi
    done 
    

    And you know what's the best??? IT REALLY WORKS!!! I cannot believe that I did it!!!

    Thanks a lot for your help!!!