I have the following script which checks if there is a file in a folder and if the file is stable (this is for large video file I am receiving).#!/bin/bashcdate1=$(date +%Y%m%d-%T)folder1=/path-to-foldercd $folder1while file=$(ls $folder1)      [ -z $file ]        do sleep 10        done            echo There is a file in the folder at $cdate1            size1=$(stat -c '%s' $file)            echo The size1 is $size1            sleep 30            size2=$(stat -c '%s' $file)            echo The size2 is $size2        if [ $size1 = $size2 ]        then            ls -l            echo Start converting        else             echo Restart the script        fiHow to modify the script so it can check multiple files in the same folder and after all of them are stable to start the conversion script?
Wait for multiple files to be finished downloading
bash;shell script;files
there are a number of problems in your script, starting with:folder1=path-to-foldercd $folder1while file=$(ls $folder1) ...this really does cd path-to-folder; ls path-to-folder; if path-to-folder is absolute (starting with a '/' as in your example) this might work, but it will not work whenever you use relative paths.it will also not work if path-to-folder contains spaces, as you should use quotes everywhere, e.g. cd $folder1then doing a while file=$(ls ...) will do an infinite loop, as file will always be set to something (the contents of the directory).the proper syntax would be for file in $(ls ...), which will stop working as soon as you have filenames with spaces (as the loop will run on foo and bar if you have a file named foo bar). check why you should never parse the output of ls. instead of using ls you could simply do for file in *.finally, you could have files that are changing even if there size is not changing any more.a good way to iterate over files is the find command; a good way to check whether something has changed is the mtime of a file.the following function gives you a value for the last modification (mtime) of any file in the given directoryfolder1=/path-to-folderfind ${folder1} -exec stat -c %Y \{\} \;    \   | sort -n | tail -1so your script could look like:#!/bin/bashdir=$1# check whether $dir existstest -d ${dir} || exit 1last=0current=1while [ $last != $current ]; do   last=$current   current=$(find ${dir} -exec stat -c %Y \{\} \; \             | sort -n | tail -1)   sleep 10doneecho directory is now stable...UPDATEan even better approach would be to actively notify the receiver that a given file has been transmitted.a very simple solution would be to also copy an empty dummy file after the payload has been transmitted. e.g. for a file named foo.avi copy another file foo.avi.copyfinished; so you only need to check for the existance of foo.avi.copyfinished to see that foo.avi is ready.while true; do for file_ready in *.copyfinished; do   file=${file_ready%.copyfinished}   if [ -e ${file}.converted ]; then      echo skipping already converted file ${file} 1>&2   else      touch ${file}.converted      do_convert ${file}   fi done sleep 1donethis solution obviously requires the cooperation from the sending side.