Create unique filename for fswebcam
I have it fswebcam
installed in my ubuntu. I want to fswebcam
provide output as img1, img2 (if img1 is present), img3, img4 ... etc.
I tried:
sudo fswebcam img
It stores files as img but instead of existing instead of existing instead of img2 . Is there any particular type of unix command to store the filename as I pointed out?
source to share
I would also like to know this particular command, if it exists. Meanwhile, I also needed to do this, 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 all filenames separated by a character \n
. LAST
will have nothing, or the maximum number after $PREFIX
.
Finally, the function echo
is the last filename, incremented by 1.
So, once you have defined f
(or a more significant name), you can invoke 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
source to share