LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-10-2015, 04:29 PM   #1
aq_mishu
Member
 
Registered: Sep 2005
Location: Bangladesh
Distribution: RH 7.2, 8, 9, Fedora
Posts: 217

Rep: Reputation: 30
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~
 
Old 08-10-2015, 05:02 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
Old 08-11-2015, 03:37 AM   #3
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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.

Last edited by Beryllos; 08-11-2015 at 04:20 AM. Reason: possible error message
 
1 members found this post helpful.
Old 08-11-2015, 05:33 AM   #4
aq_mishu
Member
 
Registered: Sep 2005
Location: Bangladesh
Distribution: RH 7.2, 8, 9, Fedora
Posts: 217

Original Poster
Rep: Reputation: 30
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~
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 01:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration