How to select and copy a latest file from a folder to another folder- Ubuntu Script

Background:
Once , I had a requirement to find a method  to send email alerts with an attached video of the event ,when a motion detection is occurred in Motion Linux .
According to my configuration of camera4.conf , the video clips of motion detection events are stored automatically  in a folder (motion) at ~/Videos/motion of the computer.
  1. My target was to select most resent video clip in ~/Videos/motion and to send email alert attached  with the video clip.
  2. For the purpose of easy emailing purpose  I have decided to create a sub folder called “sub” under ~/Videos/motion ( ~/Videos/motion/sub) to copy selected the latest video clip in to it.
  3. Then an Ubuntu script file was prepared to do the above two steps
I have tried two methods in two alternative script files successfully ,where the details are appended below.
The script files were placed at ~/Documents folder of the computer
Method 1
Name of the file mewinu.sh
Scripts in mewinu.sh are listed below

#!/bin/bash
rm ~/Videos/motion/sub/*.*
i=0
j=$(stat ~/Videos/motion/*.mkv --printf "%i\n" | wc -l )
for k in ~/Videos/motion/*.mkv; do
  if (( (j - ++i) < 1 )); then
    cp -v -- "$k" ~/Videos/motion/sub
  fi
done

echo "See this vieo" | mutt -s "Motion is Detected" -a /home/bkjaya1952/Videos/motion/sub/*.mkv -- bkjaya1952@aol.com

Figure:- 1 scripts in mewinu.sh
For making mewinu.sh executable, on Ubuntu terminal , run
sudo chmod +x ~/Documents/mewinu.sh
When “mewinu.sh” is executed every time , first all the files at ~/Videos/motion/sub are deleted by the following command in mewinu.sh
rm ~/Videos/motion/sub/*.*
Then the latest video is selected from folder ~/Videos/motion and copy in to the folder ~/Videos/motion/sub by the following commands in mewinu.sh

i=0
j=$(stat ~/Videos/motion/*.mkv --printf "%i\n" | wc -l )
for k in ~/Videos/motion/*.mkv; do
  if (( (j - ++i) < 1 )); then
    cp -v -- "$k" ~/Videos/motion/sub
  fi
done


 
Then an email alert is sent with an attachment  of the latest video copied in to ~/Videos/motion/sub by the following command
echo "See this vieo" | mutt -s "Motion is Detected" -a /home/bkjaya1952/Videos/motion/sub/*.mkv -- bkjaya1952@aol.com
The most important commands for selecting and coping latest file to another folder in Method 1 is marked in red as shown above  .

Usage of Method 1 is described in the following link
How to Email Motion Detection videos of Motion Linux( 3.2.2) using Mutt on Ubuntu 19.04



Method 2

Name of the file MotionTest.sh
Scripts in MotionTest.sh are listed below
#!/bin/bash
rm ~/Videos/motion/sub/*.*
cp -p "`ls -dtr1 ~/Videos/motion/*.mkv | tail -1`" ~/Videos/motion/sub
rclone copy ~/Videos/motion/sub/*.mkv gdrv:camera

echo "web link of the folder where the the videos are stored in drivehq.com" | mutt -s "Motion is detected see the link" -- bkjaya1952@aol.com



For making MotionTest.sh executable, on Ubuntu terminal , run
sudo chmod +x ~Documents/mewinu.sh


Then the latest video is selected from folder ~/Videos/motion and copy in to the folder ~/Videos/motion/sub by the following command in MotionTest.sh
 cp -p "`ls -dtr1 ~/Videos/motion/*.mkv | tail -1`" ~/Videos/motion/sub



The most important commands for selecting and coping latest file to another folder in Method 2  is marked in green  as shown above  .

Then the latest video is uploaded to free ftp cloud DriveHQ (named as gdrv in rclone)using the rclone  software.
And then, an  email alert is sent with an attachment  of the link of the the latest video uploaded to gdrv  by the following command .
echo "See this vieo" | mutt -s "Motion is Detected" -a /home/bkjaya1952/Videos/motion/sub/*.mkv -- bkjaya1952@aol.com


 Figure:- 2  scripts in MotionTest.sh

For making MotionTest.sh executable, on Ubuntu terminal , run
sudo chmod +x ~/Documents/mewinu.sh
Then the latest video is selected from folder ~/Videos/motion and copy in to the folder ~/Videos/motion/sub by the following command in MotionTest.sh\
cp -p "`ls -dtr1 ~/Videos/motion/*.mkv | tail -1`" ~/Videos/motion/sub

The most important commands for selecting and coping latest file to another folder in Method 2  is marked in green  as shown above  .
Then the latest video is uploaded to free ftp cloud DriveHQ (named as gdrv in rclone)using the rclone  software.
And then, an  email alert is sent with an attachment  of the link of the the latest video uploaded to gdrv  by the following command .

echo "See this vieo" | mutt -s "Motion is Detected" -a /home/bkjaya1952/Videos/motion/sub/*.mkv -- bkjaya1952@aol.com
Use of Method 2 is discribed in this link 

 

Comments

Popular posts from this blog

How to install ivms4200-linux docker image on Ubuntu 19.10 (eoan)

how to use bkjaya1952/smath-studio (a docker repository of SMath Studio-0.99.7030) on Ubuntu 19.10 (eoan)

A Solution to the installation problem of ZONEMINDER,v1.33.14.ON UBUNTU 19.10 (Eoan Ermine) using Mriadb instead of mysql-server-8.0