The following script copies a directory and all of its contents to another directory
import shutil
import os
import argparse
def copy_directory(src, dst):
"""
This function copies a directory, including all of its content and hidden files, to another directory.
Args:
src (str): The source directory.
dst (str): The destination directory.
Returns:
None
"""
# Create the destination directory path including the source directory name
dst = os.path.join(dst, os.path.basename(src))
# Ensure the destination directory exists
os.makedirs(dst, exist_ok=True)
# Iterate over all the files in source directory
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
# If item is a directory, recurse into it
if os.path.isdir(s):
print(f"Copying directory {s} to {d}")
copy_directory(s, d)
else:
# Otherwise, copy it
print(f"Copying file {s} to {d}")
shutil.copy2(s, d)
def main():
parser = argparse.ArgumentParser(description='Copy a directory including all of its content and hidden files to another directory.')
parser.add_argument('src', type=str, help='The source directory')
parser.add_argument('dst', type=str, help='The destination directory')
args = parser.parse_args()
copy_directory(args.src, args.dst)
if __name__ == "__main__":
main()
Call the script like this
python3 copy_dir.py /path/to/source/directory /path/to/destination/directory