Search code examples
macoscommand-linediff

How to list the files and folders that are not present in each other without comparing the contents of the file?


I am currently using diff -rq /dir1 /dir2 to check for contents that are not present in each other. But this compares the contents of the file which takes forever if using a disc and hard drive. I want to list differences only by checking the name. How to do that in mac? Is there any tool that can do this? Do any nifty custom script? Thanks.


Solution

  • I was able to check with find and comp.

    #!/bin/bash
    
    if [ $# -ne 2 ]; then
        echo "Usage: $0 <folder1> <folder2>"
        exit 1
    fi
    
    folder1_path="$1"
    folder2_path="$2"
    
    # List all files in first folder
    find "$folder1_path" -type f | sed "s#${folder1_path}/##" | sort > files_in_folder1.txt
    
    # List all files in second folder
    find "$folder2_path" -type f | sed "s#${folder2_path}/##" | sort > files_in_folder2.txt
    
    echo 'Files in folder1 that are not in folder2:'
    comm -23 files_in_folder1.txt files_in_folder2.txt
    
    echo ''
    echo 'Files in folder2 that are not in folder1:'
    comm -23 files_in_folder2.txt files_in_folder1.txt
    
    # Clean up
    rm files_in_folder1.txt files_in_folder2.txt