First it is my first time writing a bash script so I apologize if this is trivial. I am trying to setup a watch so every time a jpg file is uploaded to a specific folder, it gets converted to webp using cwebp. after googling the web, it seemed that using inotifywait is the best way (please let me know if that is not correct). reading bash script manual and inotifywait page I managed to write this code:

inotifywait -m /home/ben -e create -e moved_to |
    while read path action file; do
        # echo "The file '$file' appeared in directory '$path' via '$action'"
        if [[ $file = *.jpg ]]
             cwebp $file -o $file.webp

this works when I use for example mv command but when using the code above, I get this error:

Could not read 0 bytes of data from file test.jpg
Error! Could not process file test.jpg
Error! Cannot read input picture file 'test.jpg'

if I run the command cwebp test.jpg -o test.jpg.webp separately, it executes without any errors. what am I doing wrong? this triggers after the file is created, why cwebp is getting 0 bytes?

  • 3
    Maybe the file had been opened (created) but not yet written to? Try close_write instead?
    – Jeff Schaller
    Jan 8, 2016 at 15:40
  • Side note: You should quote your variables and check the script with shellcheck.
    – Marco
    Jan 8, 2016 at 16:05
  • Jeff, Thank you. close_write was the right event to use. Marco, thank you for the tip Jan 8, 2016 at 17:30

1 Answer 1


The inotifywait script you had was using -e create instead of -e close_write; the difference is that the create event will fire off before data has been written to the file; thus, cwebp had "0 bytes of data from file".

From the inotifywait page you referenced:

  • create

A file or directory was created within a watched directory.

  • close_write

A watched file or a file within a watched directory was closed, after being opened in writeable mode. This does not necessarily imply the file was written to.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .