LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script (https://www.linuxquestions.org/questions/linux-software-2/bash-script-4175550414/)

aq_mishu 08-10-2015 04:29 PM

bash script
 
Guys,
A bit confused... can't figure out how... can somebody be my angel guide???


I am taking images from a cam as an snapshot. it's sending me files. Now, I need a bash script that will rename the latest file (file names are hard to guess based on sequence numbers... I can only give partial name like 192.168.0.100-* where the names are IP+date+time+many other things...) to mycam1image.jpeg

and then delete existing old files... like rm -rf 192.168.0.1-*

Thus only the latest file will be replaced and previous files will be removed. Now, i must detect the latest file, as if for some case there are multiple, then i have to use the latest under any cost.

and it needs to be run in a cronjob (the script) on a per minute (or 5 mins ) basis.

Any ideas???


Mishu~

Keith Hedger 08-10-2015 05:02 PM

you can use the find command to find by date you can also use the stat command to get file info like size and date have a look at the man pages for those commands, also show what you have tried and also a listing of the folder containing the image filesnso we have a better idea of the format of the file names, the more info you give the easier it is for us to help

Beryllos 08-11-2015 03:37 AM

There are many ways to do it. If you only receive pictures from the IP address 192.168.0.1, you could use the following script and invoke it from cron:
Code:

#!/bin/bash

cd /directory/where/the/files/may/exist

if [ $(ls 192.168.0.1*.jpeg 2>/dev/null | wc -l) -ge 2 ] # do not delete files unless two or more exist
then
    rm $(ls -rt 192.168.0.1*.jpeg | head --lines=-1) # list in order of modtime, select all but the most recent, delete
fi

if [ $(ls 192.168.0.1*.jpeg 2>/dev/null | wc -l) -ge 1 ] # do not mv file unless one (or more) exists
then
    mv $(ls -rt 192.168.0.1*.jpeg | tail --lines=1) mycam1image.jpeg # mv most recent file to target filename
fi

If there are no new webcam images, the script will do nothing.
If there are two or more new images, the script will delete all but the most recent.
The newest webcam image will be renamed, overwriting the target file if necessary.
If new webcam images appears after the rm and before the mv, the script will mv the most recent.

Edit: There is one problem. If there are no new files, the script generates error messages ("ls: cannot access 192.168.0.1*.jpeg: No such file or directory"), though it correctly does nothing else. The error messages can be discarded with 2>/dev/null as I have added above. There may be a more elegant method which does not generate or require special handling of these error message.

aq_mishu 08-11-2015 05:33 AM

that works so far... thanks a lot for the assistance...

it removes all the files... and keeps the renamed one... my camera takes 5 pics in 1 sec interval on every 5 mins. and then the cron job executes it on 6th min (cron runs per minute). one min lag from the cam time stamp... but who cares??

Thanks again Truly

Mishu~


All times are GMT -5. The time now is 06:38 PM.