Bash

如何找到相同的文件名但不同的內容

  • May 1, 2020

我正在嘗試查找具有相同名稱但包含來自兩個不同目錄的不同內容的文本文件。

以下是我的程式碼,但它一直在這裡崩潰

cati=`ls $1 | cat $i`

catj=`ls $2 | cat $j` 

ETC…

我該如何糾正?

#!/bin/bash

if [ $# -ne 2 ];then
   echo "Usage ./search.sh dir1 dir2"
   exit 1
elif [ ! -d  $1 ];then
   echo "Error cannot find dir1"
   exit 2
elif [ ! -d  $2 ];then
       echo "Error cannot find dir2"
       exit 2
elif [ $1 == $2 ];then
   echo "Error both are same directories"

else
   listing1=`ls $1`
   listing2=`ls $2`
   for i in $listing1; do
           for j in $listing2; do
               if [ $i == $j ];then
                    cati=`ls $1 | cat $i`
                    catj=`ls $2 | cat $j`
                    if [ $cati != $catj ];then
                          echo $j
                    fi
               fi

           done
   done
fi

這是另一個使用findwhile read loop

#!/usr/bin/env bash

##: Set a trap to clean up temp files.
trap 'rm -rf "$tmpdir"' EXIT

##: Create temp directory using mktemp.
tmpdir=$(mktemp -d) || exit

##: If arguments less than 2
if (( $# < 2 )); then
 printf >&2 "Usage %s dir1 dir2 \n" "${BASH_SOURCE##*/}"
 exit 1
fi

dirA=$1
dirB=$2

##: Loop through the directories
for d in "$dirA" "$dirB"; do
 if  [[ ! -e $d ]]; then  ##: If directory does not exists
   printf >&2 "%s No such file or directory!\n" "$d"
   exit 1
 elif [[ ! -d $d ]]; then  ##: If it is not a directory.
   printf >&2 "%s is not a directory!\n" "$d"
   exit 1
 fi
done

##: If dir1 and dir2 has the same name.
if [[ $dirA = $dirB ]]; then
 printf >&2 "Dir %s and %s are the same directory!\n" "$dirA" "$dirB"
 exit 1
fi

##: Save the list of files in a file using find.
find "$dirA" -type f -print0 > "$tmpdir/$dirA.txt"
find "$dirB" -type f -print0 > "$tmpdir/$dirB.txt"


##: Although awk is best for comparing 2 files I'll leave that to the awk masters.

while IFS= read -ru "$fd" -d '' file1; do  ##: Loop through files of dirB
 while IFS= read -r -d '' file2; do ##: Loop through files of dirA
   if [[ ${file1##*/} = ${file2##*/} ]]; then  ##: If they have the same name
     if ! cmp -s "$file1" "$file2"; then ##: If content are not the same.
       printf 'file %s and %s has the same name but different content\n' "$file1" "$file2"
     else
       printf 'file %s and %s has the same name and same content\n' "$file1" "$file2"
     fi
   fi
 done < "$tmpdir/$dirA.txt"
done {fd}< "$tmpdir/$dirB.txt"

引用自:https://unix.stackexchange.com/questions/583695