【问题标题】:Comparing Folder Contents in Bash - looking to compare specific digits in the folder names比较 Bash 中的文件夹内容 - 比较文件夹名称中的特定数字
【发布时间】:2018-02-06 18:10:59
【问题描述】:

我正在尝试将一个文件夹的内容与另一个文件夹的内容进行比较,我知道我可以这样做

diff -rq folder1 folder2

但是:在文件夹 1 中,内容都是名为 word_12345.1.2 的子文件夹,而文件夹 2 包含名为 12345.1.2.physio 的文件。我想查看 folder1 中的哪些文件夹我在第二个文件夹中有一个文件。如果我在文件夹 1 中有文件夹的文件,则文件夹 1 中的文件夹和文件夹 2 中的文件上的数字将匹配。

当我只使用diff -rq folder1 folder2 时,输出显示没有文件匹配,因为确切的名称不匹配。我正在努力将 glob 放在哪里让 bash 排列这些数字 ID。

谢谢!

【问题讨论】:

  • 所以folder1/word_12345.1.2 应该与folder2/12345.1.2.physio 匹配?
  • @LuisMuñoz 是的!

标签: bash shell terminal


【解决方案1】:

您可以使用以下脚本:

#!/bin/bash

echo "Folders with a matching file:"
for folder in folder1/*
do
    if [ -f "folder2/${folder#folder1/word_}.physio" ]
    then
        echo "${folder#folder1/}"
    fi
done

当在两个文件夹的父目录中执行时,会列出文件夹 1 中的那些文件夹以及文件夹 2 中的匹配文件。

要执行脚本,请将代码保存为文本文件,假设为matches_with_file.sh,然后执行chmod u+x matches_with_file.sh,并使用./matches_with_file.sh 调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2011-09-25
    相关资源
    最近更新 更多