Skip to content

Instantly share code, notes, and snippets.

@monkut
Created June 12, 2018 07:23
Show Gist options
  • Save monkut/139efcf1b7250d892c47a5dd7b4a91d1 to your computer and use it in GitHub Desktop.
Save monkut/139efcf1b7250d892c47a5dd7b4a91d1 to your computer and use it in GitHub Desktop.
Compare two directory's content size with percentage
"""
This is a script to compare the content of two directories.
It was used to determine how complete a series of cp commands were done after the cp process was already started
"""
import os
def directory_size(path):
total = 0
for entry in os.scandir(path):
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += directory_size(entry.path)
return total
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Compare the size of two directories')
parser.add_argument('-f', '--first',
required=True,
dest='first_directory',
help='The first directory to use as the basis for compare')
parser.add_argument('-s', '--second',
required=True,
dest='second_directory',
help='The second directory to compare to the first')
args = parser.parse_args()
first_directory_size = directory_size(os.path.abspath(os.path.expanduser(args.first_directory)))
second_directory_size = directory_size(os.path.abspath(os.path.expanduser(args.second_directory)))
percentage_difference = round(100 * (second_directory_size/first_directory_size), 2)
print('{}({})/{}({}): {}%'.format(args.second_directory, second_directory_size, args.first_directory, first_directory_size, percentage_difference))
@monkut
Copy link
Author

monkut commented Jun 12, 2018

Used in combination with the watch command to monitor the progress of cp:

sudo watch -n 1 python3 compare_directories.py -f ./source/ -s /dest/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment