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?
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
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)
$ 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With