Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I run a cron job that requests a snapshot from a remote webcam at a local address:

wget http://user:pass@10.0.0.50/snapshot.cgi

This creates the files snapshot.cgi, snapshot.cgi.1, snapshot.cgi.2, each time it's run.

My desired result would be for the file to be named similar to file.1.jpg, file.2.jpg. Basically, sequentially or date/time named files with the correct file extension instead of .cgi.

Any ideas?

share|improve this question
    
Does this mean you get 3 files each time it's run ? –  Iain Apr 12 '11 at 7:34
    
Sorry, that wasn't clear. It creates a single new file on each run. –  Ian Apr 12 '11 at 14:21
add comment

2 Answers

up vote 4 down vote accepted

You could probably torture wget into doing it, but why bother? Try

wget http://user:pass@10.0.0.50/snapshot.cgi
mv snapshot.cgi snapshot-`date +%Y-%m-%d-%H%M%S`.jpeg

This should create date-and-time stamped images like snapshot-2011-04-12-081649.jpeg. Will that do?

Edit: OK, not too much torturing is required:

wget -O snapshot-`date +%Y-%m-%d-%H%M%S`.jpeg http://user:pass@10.0.0.50/snapshot.cgi

But most of me still prefers the UNIX way of doing it with small, discrete tools.

share|improve this answer
    
If you just want a simple date stamp, MadHatter's solution is the way to go. I ran with the sequential incremental counter idea. –  Phil Hollenback Apr 12 '11 at 7:29
add comment

You can do this with a simple bash loop and the -O option in wget.

Something like this:

i=0
while 1
do
  # increment our counter
  ((i++))
  # get the file and save it
  wget -O file.$i.jpg http://user:pass@10.0.0.50/snapshot.cgi
  # presumably you want to wait some time after each retrieval
  sleep 30
done

One obvious annoyance is that if you already have a file.1.jpg in the directory and you start this script, it will be overwritten. To deal with that, you first need to find all the existing file.N.jpg files, find the largest value for N, and start at N+1. Here's an incredibly braindead way to do that:

# find the last sequential file:
i=$(ls -t file.*.jpg | head -1 | awk -F. '{ print $2}') 
# if there weren't any matching files, force $i to 0
[ $i > 0 ] || i=0
# increment by one to get your starting value for $i
((i++))
# and then start your capture mechanism
while 1
do
  # increment our counter
  ((i++))
  # get the file and save it
  wget -O file.$i.jpg http://user:pass@10.0.0.50/snapshot.cgi
  # presumably you want to wait some time after each retrieval
  sleep 30
done

Really I should rewrite this whole thing as a perl one-liner but I'm tired and it's late so I'll be lazy. Anyway that should give you an idea of how to accomplish this with a simple shell script mechanism.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.