there are a number of problems in your script, starting with:
folder1="path-to-folder"
cd $folder1
while 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 "$folder1"
then 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 directory
folder1="/path-to-folder"
find "${folder1}" -exec stat -c "%Y" \{\} \; \
| sort -n | tail -1
so your script could look like:
#!/bin/bash
dir="$1"
# check whether $dir exists
test -d "${dir}" || exit 1
last=0
current=1
while [ "$last" != "$current" ]; do
last=$current
current=$(find "${dir}" -exec stat -c "%Y" \{\} \; \
| sort -n | tail -1)
sleep 10
done
echo "directory is now stable..."
UPDATE
an 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 1
done
this solution obviously requires the cooperation from the sending side.
$size1 = $size2
it will always be same , what you want to compare between file, in which basis ? can you please explain what you want to achieve