Bash loop for detecting equal folder sizes Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Bash loop for detecting equal folder sizes without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

Answer

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'
We are here to answer your question about Bash loop for detecting equal folder sizes - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji