Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- in linux bash I want to make a script that scan all files in a folder and compares them if their contents are identical
- Here is a bash script that compares all files in a folder to see if their contents are identical:
- ```bash
- #!/bin/bash
- # Get list of all files in the folder
- files=$(find /path/to/folder -type f)
- # Loop through each file
- for file1 in $files; do
- for file2 in $files; do
- if [ "$file1" != "$file2" ]; then
- if cmp -s "$file1" "$file2"; then
- echo "$file1 and $file2 have the same contents."
- fi
- fi
- done
- done
- ```
- 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.
- ---------------------------------------
- PYTHON
- in python I want to make a script that scan all files in a folder and compares them if their contents are identical
- Here's a Python script that scans all files in a folder and compares them to check if their contents are identical:
- ```python
- import os
- import filecmp
- def compare_files(folder_path):
- files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
- for i in range(len(files)):
- for j in range(i+1, len(files)):
- file1 = os.path.join(folder_path, files[i])
- file2 = os.path.join(folder_path, files[j])
- if filecmp.cmp(file1, file2):
- print(f"{files[i]} and {files[j]} have identical contents")
- else:
- print(f"{files[i]} and {files[j]} do not have identical contents")
- folder_path = "your_folder_path_here"
- compare_files(folder_path)
- ```
- 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