Created
June 12, 2018 07:23
-
-
Save monkut/139efcf1b7250d892c47a5dd7b4a91d1 to your computer and use it in GitHub Desktop.
Compare two directory's content size with percentage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used in combination with the
watch
command to monitor the progress ofcp
: