如何在Shell脚本中比较两个目录中的文件名? [英] How do I compare file names in two directories in shell script?

查看:58
本文介绍了如何在Shell脚本中比较两个目录中的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,这就是我的代码:

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 ]]; then
            echo "$file1 is contained in both directories"
        fi
    done
done

我正在尝试比较输入的两个目录的文件名,并说如果文件名匹配,则文件在两个目录中.但是,当我尝试运行它时,即使两个目录中都有相同的文件,也不会回显任何内容.

I am trying to compare the file names of the two directories entered and say that the file is in both directories if the filename matches. When I try to run it though, nothing is echo-ed even though I have the same file in both directories.

推荐答案

Dir1 Dir2 中的文件:

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

位于 Dir1 中但不在 Dir2 中的文件:

Files that are in Dir1 but not in Dir2:

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

位于 Dir2 中但不在 Dir1 中的文件:

Files that are in Dir2 but not in Dir1:

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

这篇关于如何在Shell脚本中比较两个目录中的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆