简体   繁体   中英

How to output difference of files from two folders and save the output with the same name on different folder

I have two folders which have same file names, but different contents. So, I am trying to generate a script to get the difference and to see what is being changed. I wrote a script below :

folder1="/opt/dir1"
folder2=`ls/opt/dir2`

find "$folder1/" /opt/dir2/ -printf '%P\n' | sort | uniq -d

for item in `ls $folder1`
do
 if [[ $item == $folder2 ]]; then
  diff -r $item $folder2 >> output.txt
 fi
done

I believe this script has to work, but it is not giving any output on output folder. So the desired output should be in one file . Ex:

cat output.txt

diff -r /opt/folder1/file1 /opt/folder2/file1
1387c1387
<               ALL X'25' BY SPACE                                                
---

>               ALL X'0A' BY SPACE      

diff -r /opt/folder1/file2 /opt/folder2/file2
2591c2591
<               ALL X'25' BY SPACE                                                
---
>               ALL X'0A' BY SPACE 

Any help is appreciated!

Ok. So twofold:

  1. First get the files in one folder. Never use ls . Forget it exists. ls is for nice printing in our console. In scripts, use find .
  2. Then for each file do some command. A simple while read loop.

So:

{
    # make find print relative to `/opr/dir1` director
    cd /opt/dir1 &&
    # Use `%P` so that print without leading `./`
    find . -mindepth 1 -type f -print "%P\n"
} |
while IFS= read -r file; do
     diff /opt/dir1/"$file" /opt/dir2/"$file" >> output/"$file"
done

Notes:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM