Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate unique filename for fswebcam

Tags:

shell

unix

ubuntu

I have fswebcam installed in my ubuntu. I want the fswebcam to provide output as img1, img2 (if img1 is there), img3, img4... etc

I tried:

sudo fswebcam img

It stores the files as img but replaces the existing one instead of storing as img2.
Is there any specific type of unix command to store the filename as I specified?

like image 590
Prasanna Anbazhagan Avatar asked Jan 05 '15 02:01

Prasanna Anbazhagan


2 Answers

Or you could just use its built in strftime capability to generate each file with the current time in the filename

--save pic%Y-%m-%d_%H:%M:%S.jpg
like image 62
pootle Avatar answered Nov 06 '22 21:11

pootle


I would also like to know that specific command if it exists. Meanwhile, I also needed to do that, and I used a workaround as follows (adapted to your needs):

f() {
    PREFIX="./img"
    FILES=$(ls $PREFIX* 2> /dev/null)
    LAST=$(sort -n <<<"${FILES//$PREFIX}" | tail -n1)
    echo $PREFIX$((LAST+1))
}

FILES contains al the filenames separated by \n.
LAST will have nothing, or the max number after the $PREFIX.
Finally, the function echo'es the last filename incremented by 1.

So, once you have defined f (or a more significant name), you can call your command like this:

sudo fswebcam $(f)

Example

$ ls
img1  img10  img11  img2  img3  img4  img5  img6  img7  img8  img9
$ echo $(f) # here I'm using "echo" instead of "sudo fswebcam"
./img12

f step by step

$ FILES=$(ls $PREFIX* 2> /dev/null)
$ cat <<<"$FILES"
./img1
./img10
./img11
./img2
./img3
./img4
./img5
./img6
./img7
./img8
./img9
$ LAST=$(sort -n <<<"${FILES//$PREFIX}" | tail -n1)
$ echo $LAST
11
$ echo $PREFIX$((LAST+1))
./img12
like image 20
whoan Avatar answered Nov 06 '22 20:11

whoan