How do I compare file names in two directories in shell script?

前端 未结 5 835
有刺的猬
有刺的猬 2021-01-31 05:39

Right now, this is what my code looks like:

#!/bin/bash

Dir1=$1
Dir2=$2

for file1 in $Dir1/*; do
    for file2 in $Dir2/*; do
        if [[ $file1 == $file2 ]]         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 06:01

    Files that are in both Dir1 and Dir2:

    find "$Dir1/" "$Dir2/" -printf '%P\n' | sort | uniq -d
    

    Files that are in Dir1 but not in Dir2:

    find "$Dir1/" "$Dir2/" "$Dir2/" -printf '%P\n' | sort | uniq -u
    

    Files that are in Dir2 but not in Dir1:

    find "$Dir1/" "$Dir1/" "$Dir2/" -printf '%P\n' | sort | uniq -u
    

提交回复
热议问题