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.
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
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
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
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
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.
- My target was to select most resent video clip in ~/Videos/motion and to send email alert attached with the video clip.
- 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.
- Then an Ubuntu script file was prepared to do the above two steps
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.shWhen “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 commandecho "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
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
Post a Comment