I wrote a script to find the files which are not present between two folders. Initially I used diff
which works fine for source code but when I want to just check for folders and files newly added to a directory without having to compare its content, diff
is very slow. I wrote it mainly for backup purposes. For example, I have documents written to optical disk for archiving. I want to know which are the newly added files since the archival. So I can quickly compare the folder from optical disk and the hard drive to get a list of file I need to backup. Checking content takes ages.
#!/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
Script for checking content difference using diff
#!/bin/bash
# find difference between file and folder contents
diff -rq "$1" "$2"