1

I would like to detect when the transfer of a large amount of files is completed. I would like to accomplish this by detecting the size of the folder with a time delay.

Below is what I have done,

  #!/bin/bash

firstSize= du -s /Users/test/Desktop/folder | cut -f1

sleep 3
newSize= du -s /Users/test/Desktop/folder | cut -f1


until [ $firstSize -eq $newSize ]
do
    firstSize=$newSize
    sleep 3
    newSize= du -s /Users/test/Desktop/folder | cut -f1

done

echo 'Done'

The until loop is not working because even when the firstSize and the newSize are not equal the loop completes. I am not familiar with writing Bash scripts so I am just making mistakes. This loop is ported over from an applescript I had wrote for the same purpose but I need something more reliable.

  • 2
  • 1
    Can't you measure the file size rather than the directory size? Could your sending process send the file with a temporary suffix (eg .tmp) and then rename it on successful transfer? (Does OSX have inotify? If so that can provide a far more efficient way of determining that a file has landed.) – Chris Davies Sep 16 '18 at 21:37
  • Hi @lightwalker. Would you please add more clarifications about what you would like to achieve, so we can help you more efficiently!! How do you transfer your files between folders? do you use rsync, scp, ... ect ? What is the source folder and what is the target folder? Do you want to detect the equality between the source and target folders? let's say that you are using rsync to transfer files between folder A to folder B, let's say the the folders sizes A=B what next? what is the purpose of the script? –  Sep 16 '18 at 21:57
  • Thanks @steeldriver that link helped and it is now working. – lightwalker Sep 16 '18 at 22:34
  • @roaima thanks, the reason for the folder rather than the file is I have do not have control over the file. – lightwalker Sep 16 '18 at 22:35
  • Thanks @Goro. I am monitoring the folder with launchd for changes before I execute a script to sync the changes across multiple devices and I don't want to trigger the script to soon. – lightwalker Sep 16 '18 at 22:35
  • @Jeff Schaller sorry yes I missed that last sanitisation. – lightwalker Sep 16 '18 at 23:38

1 Answers1

0

You messed the commands syntax. The script should look like:

#!/bin/sh -
firstSize=$(du -s /Users/test/Desktop/folder | cut -f1)
until
  sleep 3
  newSize=$(du -s /Users/test/Desktop/folder | cut -f1)
  [ "$firstSize" -eq "$newSize" ]
do
    firstSize=$newSize
done
echo 'Done'
Romeo Ninov
  • 17,484