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?

+3


source to share


3 answers


location="your location directory"
cd $location
ls
fname='filename'
i=1
j=1
    sudo fswebcam $fname 
    if [ -f $fname ]
    then
        echo "exist"
        while [ -f $fname$i ]
        do
            echo $fname$i
            i=$((i + j))
        done
        mv $fname $fname$i
    else
        echo "oops not found"
    fi

      



0


source


Or you can just use the built-in strftime function to generate each file with the current time in the filename



--save pic%Y-%m-%d_%H:%M:%S.jpg

      

+7


source


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

      

+1


source







All Articles