0

I have video file "test.mp4" which I am downloading using wget. It's more than 100MB+. I want to keep checking the file size and execute certain commands on it after it increments every +2MB or some custom limits. Presently I am trying with nested ifs and while loops:

while true;
do
    if [[ $(find /home/user/test.mp4 -type f -size +200k 2>/dev/null);
        then
        ##### executre some commands
        while true;
        do
            if [[ $(find /home/user/test.mp4 -type f -size +2000k 2>/dev/null) ]];
                then
                ##### executre some commands
                while true;
                do
                    if [[ $(find /home/user/test.mp4 -type f -size +4000k 2>/dev/null) ]];
                        then
                        ##### executre some commands
                        while true;
                        do
                            if [[ $(find /home/user/test.mp4 -type f -size +6000k 2>/dev/null) ]];
                                then
                                ##### executre some commands
                                ##### I have to write while loops like this for ever -----------
                                break;
                            fi
                        done;
                        break;
                    fi
                done;
                break;
            fi
        done;
        break;
    fi
done;

But I have to manually do. Is there a way it keeps checking and I can tell some limit and after that it can execute a command?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Santhosh
  • 9,965
  • 20
  • 103
  • 243

1 Answers1

0

This might do the trick for you in bash. It essentially constantly checks the filesize and compares it with a reference number. If the file size is bigger then the reference, you can execute the command. After command execution you can update the reference size.

# delta jump size
dsize=$(( 2*1024*1024*1024 )) # delta jump size (2Mb)
# current file size
fsize=$(stat --printf="%s" /home/user/test.mp4)
# reference size, set to the next upcomming n*dsize
osize=$(( ((fsize/dsize)+1)*dsize ))

while true; do
  # get file size in bytes
  fsize=$(stat --printf="%s" /home/user/test.mp4)

  # check if file size is bigger then reference size
  if (( fsize > osize )); then
      ## execute some commands

      # increment reference size
      osize=$(( osize + dsize ))
  fi

  sleep 1

done

This script can be improved in the following ways making use of this. Please take into account that during the time the executed commands run, you already downloaded more then twice the delta size. You might be interested to replace the increment reference size by

fsize=$(stat --printf="%s" /home/user/test.mp4)
osize=$(( ((fsize/dsize)+1)*dsize ))
kvantour
  • 25,269
  • 4
  • 47
  • 72