Advertisement
plirof2

compare files in folder bash,python

Mar 15th, 2024
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. in linux bash I want to make a script that scan all files in a folder and compares them if their contents are identical
  2.  
  3. Here is a bash script that compares all files in a folder to see if their contents are identical:
  4.  
  5. ```bash
  6. #!/bin/bash
  7.  
  8. # Get list of all files in the folder
  9. files=$(find /path/to/folder -type f)
  10.  
  11. # Loop through each file
  12. for file1 in $files; do
  13.   for file2 in $files; do
  14.     if [ "$file1" != "$file2" ]; then
  15.       if cmp -s "$file1" "$file2"; then
  16.         echo "$file1 and $file2 have the same contents."
  17.       fi
  18.     fi
  19.   done
  20. done
  21. ```
  22.  
  23. In the script above, replace `/path/to/folder` with the actual path to the folder you want to scan. The script will compare all files in the folder to check if their contents are identical and print a message for each pair of files that have the same contents.
  24.  
  25.  
  26. ---------------------------------------
  27. PYTHON
  28.  
  29. in python I want to make a script that scan all files in a folder and compares them if their contents are identical
  30. Here's a Python script that scans all files in a folder and compares them to check if their contents are identical:
  31.  
  32. ```python
  33. import os
  34. import filecmp
  35.  
  36. def compare_files(folder_path):
  37.    files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
  38.    
  39.    for i in range(len(files)):
  40.        for j in range(i+1, len(files)):
  41.            file1 = os.path.join(folder_path, files[i])
  42.            file2 = os.path.join(folder_path, files[j])
  43.            if filecmp.cmp(file1, file2):
  44.                print(f"{files[i]} and {files[j]} have identical contents")
  45.            else:
  46.                print(f"{files[i]} and {files[j]} do not have identical contents")
  47.  
  48. folder_path = "your_folder_path_here"
  49. compare_files(folder_path)
  50. ```
  51.  
  52. Replace `"your_folder_path_here"` with the path of the folder you want to scan. This script will compare all files in the folder and print a message indicating whether their contents are identical or not.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement