I'm completely new to bash. I have a requirement that needs to do the following:
- Iterate through a directory A's and directory B's folders with the same name
- Find two files that have the same name and compare them (im using diff <(file1) <(sort file2) to compare the files)
- If there no differences delete the file in directory A
- If there are differences ignore and process the next matching pair of files
- check the next folder from each directory and repeat the process until all matching folders have been checked.
So for example in Directory A I have folderA that has 2 files (file1 and file2) In directory B I have folderA that has 3 files (file1 and file2 and file3)
- File1 in both directories are the same - Delete from directory A
- file2 there are differences - keep in both directories
- file3 do nothing - keep in directory B
The files that I'm using are xml files. The ordering of tags sometimes differ in the files but the content would be exactly the same, unless there are additions which I'd want to keep the file. I don't necessarily care if the ordering of the tags are different I just want to make sure that all the content are the same or different. Hope that provides more clarity.
Any help would be much appreciated.
UPDATE:
So I've managed to get this far but when running the script the out put in the console is blank. It should list the files that have been found to be the same and remove them, where am I going wrong?
declare -a my_array
shopt -s globstar
cd /mnt/c/filediff/validation/applications/
for file in **; do
if [ -d "$file" ]; then
echo "$file is a directory, skipping.";
else
fileName=${file#*/}
if [[ -e /mnt/c/filediff/package/"$fileName" ]]; then
echo diff -q <(sort "$file") <(sort /mnt/c/filediff/package/"$fileName") &&
my_array=("${my_array[@]}" "$fileName")
#rm /mnt/c/filediff/package/"$fileName"
fi
fi
done
echo -e '\nRemoved the following files -----------------------------------'
for item in "${my_array[@]}"
do
echo "ITEM: *** $item ***"
done
shopt -s globstar; for f in /mnt/c/filediff/validation/applications/**; do echo "F: $f"; done
? Does that list any files?