0

I never do bash scripts so I have no clue the most efficient way to do this and quickly. I know how I would do it in something like python or c++.

I have a file structure like the one below:

-parentDir --subDir1 ---file1.txt ---file2.txt ---file3.txt ---file4.txt --subDir2 ---file1.txt ---file2.txt ---file3.txt ---file4.txt 

There could be any number of subdirectories and text files.

Basically I would like to create a bash script that goes into each subdirectory and then compares using file1.txt and file2.txt using diff and then compare file2.txt and file3.txt and so on outputting the difference to the end of a txt file.

I know how to use diff to compare files and then outputting the difference to a txt file I just do not know how to do what I envision as a double for loop.

Any ideas?

9
  • It's not a double for loop. You're just comparing each file to the next file, not to all the other files. Or did you describe it wrong? Commented Aug 1, 2019 at 0:03
  • Is it always just 2 levels of nesting, or do you have to do it recursively for any depth? Commented Aug 1, 2019 at 0:04
  • Maybe you could show us how you would do it in python then. Because otherwise your description is lacking clarty like (should you compare files across sub-directories?, are-there only text files? are text files always with same .txt suffix? where to output the diffs, with what names...) Commented Aug 1, 2019 at 0:04
  • I was thinking a double for only because I see it something like "for each dir the parent directory and then for each file in dir compare to the next file" @Bamar Commented Aug 1, 2019 at 0:05
  • Put the filenames in an array, then you can iterate over the array indexes, and use diff ${array[$i]} ${array[$i+1]} Commented Aug 1, 2019 at 0:06

1 Answer 1

2
#!/usr/bin/env bash typeset -r diffs=diffs.txt typeset -a allfiles=() typeset -- filename='' # fills the allfiles array with all *.txt files except the diffs.txt # that can be found from the current directory and down all sub-directories while IFS= read -r -d '' filename; do allfiles+=("$filename") done < <( find . -type f -name '*.txt' -and -not -name "$diffs" -print0 2>/dev/null ) [[ ${#allfiles[@]} -lt 2 ]] && exit 2 # Need at least 2 files to compare typeset -i i=0 j=0 typeset -- file_a='' file_b='' export LC_MESSAGES=POSIX # for all files except last for ((i = 0; i < ${#allfiles[@]} - 1; i++)); do file_a="${allfiles[$i]}" # for next file to last file for ((j = i + 1; j < ${#allfiles[@]}; j++)); do file_b="${allfiles[$j]}" diff --report-identical-files --unified=0 --minimal -- \ "$file_a" "$file_b" 2>/dev/null echo done done >"$diffs" # all output to the diffs file 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.