给定两个目录树,仅考虑满足条件的文件名,如何找到相同的文件名? [英] Given two directory trees how to find which filenames are the same, considering only filenames satisfying a condition?

查看:28
本文介绍了给定两个目录树,仅考虑满足条件的文件名,如何找到相同的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个答案告诉我如何在 bash 的两个目录中查找具有相同文件名的文件:

This answer tells me how to find the files with the same filename in two directories in bash:

diff -srq dir1/ dir2/ | grep identical

现在我想考虑满足条件的文件.如果我使用 ls E*,我会得到以 E 开头的文件.我想用上面的命令做同样的事情:给我在 dir1/ 中不同的文件名和 dir2/,但只考虑以 E 开头的那些.

Now I want to consider files which satisfy a condition. If I use ls E*, I get back files starting with E. I want to do the same with the above command: give me the filenames which are different in dir1/ and dir2/, but consider only those starting with E.

我尝试了以下方法:

diff -srq dir1/E* dir2/E* | grep identical

但是没有用,我得到了这个输出:

but it did not work, I got this output:

diff: 额外的操作数 '/home/pal/konkoly/c6/elesbe3/1/EPIC_212291374-c06-k2sc.dat.flag.spline' diff: 试试 'diff --help' 了解更多信息.

diff: extra operand '/home/pal/konkoly/c6/elesbe3/1/EPIC_212291374- c06-k2sc.dat.flag.spline' diff: Try 'diff --help' for more information.

((/home/pal/konkoly/c6/elesbe3/1/EPIC_212291374-c06-k2sc.dat.flag.spline是所谓的dir1中的一个文件,但是EPIC_212291374-c06-k2sc.dat.flag.spline 不在所谓的dir2))

((/home/pal/konkoly/c6/elesbe3/1/EPIC_212291374- c06-k2sc.dat.flag.spline is a file in the so-called dir1, but EPIC_212291374- c06-k2sc.dat.flag.spline is not in the so-called dir2))

我该如何解决这个问题?

How can I solve this?

根据这个答案,我尝试通过以下方式进行操作:

I tried doing it in the following way, based on this answer:

DIR1=$(ls dir1)
DIR2=$(ls dir2)

for i in $DIR1; do
    for j in $DIR2; do
        if [[ $i == $j ]]; then
            echo "$i == $j"
        fi
    done
done

它的工作原理如上,但如果我写 DIR1=$(ls path1/E*)DIR2=$(ls path2/E*),它会没有,我没有输出.

It works as above, but if I write DIR1=$(ls path1/E*) and DIR2=$(ls path2/E*), it does not, I get no output.

推荐答案

这是未经测试的,但我会尝试类似:

This is untested, but I'd try something like:

comm -12 <(cd dir1 && ls E*) <(cd dir2 && ls E*)

基本思路:

  • dir1 中生成满足我们条件的文件名列表.这可以通过 ls E* 来完成,因为我们只处理一个简单的文件列表.对于子目录和递归,我们将使用 find 代替(例如 find . -name 'E*' -type f).

  • Generate a list of filenames in dir1 that satisfy our condition. This can be done with ls E* because we're only dealing with a flat list of files. For subdirectories and recursion we'd use find instead (e.g. find . -name 'E*' -type f).

将文件名按规范顺序排列(例如通过对它们进行排序).我们不必在这里做任何事情,因为 E* 无论如何都会按排序顺序展开.使用 find,我们可能必须先将输出通过管道传送到 sort.

Put the filenames in a canonical order (e.g. by sorting them). We don't have to do anything here because E* expands in sorted order anyway. With find we might have to pipe the output into sort first.

dir2 做同样的事情.

只输出两个列表共有的行,这可以用 comm -12 来完成.

Only output lines that are common to both lists, which can be done with comm -12.

comm 期望在命令行上传递两个文件名,因此我们使用 <( ... ) bash 功能来生成子进程并连接其输出到命名管道;然后可以将管道的名称赋予 comm.

comm expects to be passed two filenames on the command line, so we use the <( ... ) bash feature to spawn a subprocess and connect its output to a named pipe; the name of the pipe can then be given to comm.

这篇关于给定两个目录树,仅考虑满足条件的文件名,如何找到相同的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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