Search code examples
pythonbashpython-3.xrsync

How to count the total number of files to be synced using rsync?


I am trying to read the total number of the files to be synced using 'rsync', and read the value using the following python code, I get the following output. What code should I modify to get the desired output

Output

b'10'

Desired Output

10


cmd

rsync -nvaz --delete --stats user@host:/www/ . | ./awk.sh

awk.sh

awk '\
BEGIN {count = 0}
  /deleting/ {if ( length($1) > 0 ) ++count} \
  /Number of regular files transferred: / {count += $6} \
END \
  {
    printf "%d",count
  }'

Python

subprocess.check_process(cmd, shell=True, stdout=False) 

Solution

  • Decoded the output to utf-8, and then parsed using RegEx

    o = subprocess.check_output(cmd, shell=True)
    g = re.search(r'count=(\d+)', o.decode("utf-8"), re.M|re.I)