AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 899774
Accepted
ethan
ethan
Asked: 2017-04-04 07:11:10 +0800 CST2017-04-04 07:11:10 +0800 CST 2017-04-04 07:11:10 +0800 CST

递归比较两个目录之间特定文件的计数

  • 772

我已经搜索和搜索,但没有找到这个问题的答案。

这是场景:我刚刚将我的整个 CD 收藏翻录到免费的无损音频编解码器 (FLAC) 以进行存档。我在这个档案中也有一些高质量的有损 .m4a 或 .mp3。这存储在 DIRECTORY01/artist - album/*.flac 结构中。此目录中的所有相关文件都是 .flac、.m4a 或 .mp3

然后我将整个目录转码为 ogg vorbis,用于实际的日常使用和我的便携式媒体播放器。这存储在 DIRECTORY02/artist - album/*.ogg 结构中。所有相关文件都是 .ogg。

现在我想验证一切都很好。我使用 diff 来比较文件夹的数量(并验证它们是否相同。然后我计算了每个文件夹中的 flac 和 ogg 文件的数量。

问题: .ogg 文件夹中有三个额外的 .ogg 文件,不知何故,某处。由于每个父目录包含 526 个子文件夹,我想知道如何自动识别仅在特定 .ogg 或 .flac/.m4a/.mp3 文件类型的文件数上不同的目录(忽略任何 .txt、.log、. nfo、.cue、.jpg 等)。

所以理想情况下,无论我运行什么命令,结果都将是一个忽略具有常见文件计数的常见子目录的报告,并帮助我明确识别具有不同“.ogg 或 .flac/.m4a/.mp3”文件的任何目录计数。

这可能吗?我也愿意接受其他建议/逻辑来验证所有 X 个 .flac/.m4a/.mp3 是否已成功转码为 .ogg。

directory
  • 3 3 个回答
  • 374 Views

3 个回答

  • Voted
  1. terdon
    2017-04-04T07:57:58+08:002017-04-04T07:57:58+08:00

    由于您拥有.ogg超过.flac,一个简单的方法是查找所有*.ogg名称并检查哪些名称没有对应的.flac名称。就像是:

    find DIRECTORY02/ -type f -name '*ogg' -print0 | 
        while IFS= read -r -d '' f; do 
            flac="${f//.ogg/.flac}"; 
            flac="${flac##DIRECTORY02/}"; 
            [[ -e DIRECTORY01/"$flac" ]] || 
                printf "Missing file: %s\n" "$flac"; 
    done
    

    这与注释脚本相同:

    #!/bin/bash
    
    ## find all files in DIRECTORY02/ whose name ends in .ogg
    find DIRECTORY02/ -type f -name '*.ogg' -print0 | 
        ## Iterate over the results of the find command, saving
        ## each file as "$f". The fancy -print0 and read -d '' stuff
        ## is needed to deal with filenames that can contain newlines.
        while IFS= read -r -d '' f; do 
            ## create the new $flac variable which is $f but with ".flac"
            ## instead of ".ogg"
            flac="${f//.ogg/.flac}"; 
            ## remove the "DIRECTORY02/" from the $flac variable. If
            ## the "$f" variable was 'DIRECTORY02/artist - album/file.ogg'
            ## it is now 'artist - album/file.flac'.
            flac="${flac##DIRECTORY02/}"; 
            ## Check whether the file exists in the same subdirectory under
            ## DIRECTORY01
            [[ -e DIRECTORY01/"$flac" ]] || 
                ## If it doesn't, print
                printf "Missing file: %s\n" "$flac"; 
    done
    
    • 3
  2. Best Answer
    Arronical
    2017-04-04T07:51:35+08:002017-04-04T07:51:35+08:00

    使用带有 的for循环find,这可能不是实现目标的最快方法,但应该可以正常工作:

    for dir in DIRECTORY01/*/ ; do fcount=$(find "$dir" -maxdepth 1 -type f \( -name '*.flac' -o -name '*.m4a' -o -name '*.mp3' \) -printf . | wc -c) ; ocount=$(find "${dir/DIRECTORY01/DIRECTORY02}" -maxdepth 1 -type f -name '*.ogg' -printf . | wc -c); if [[ "$fcount" -ne "$ocount" ]]; then echo "$dir has $fcount .flac .m4a and .mp3 files but ${dir/DIRECTORY01/DIRECTORY02} has $ocount .ogg files" ; fi ; done
    

    或者更易读

    for dir in DIRECTORY01/*/ ; do
      fcount=$(find "$dir" -maxdepth 1 -type f \( -name '*.flac' -o -name '*.m4a' -o -name '*.mp3' \) -printf . | wc -c)
      ocount=$(find "${dir/DIRECTORY01/DIRECTORY02}" -maxdepth 1 -type f -name '*.ogg' -printf . | wc -c)
      if [[ "$fcount" -ne "$ocount" ]]; then
        echo "$dir has $fcount .flac .m4a and .mp3 files but ${dir/DIRECTORY01/DIRECTORY02} has $ocount .ogg files"
      fi
    done
    

    将输出类似于:

    DIRECTORY01/Nirvana - Nevermind/ has 12 .flac files but DIRECTORY02/Nirvana - Nevermind/ has 11 .ogg files
    

    对于文件计数不匹配的每个目录。循环不会优雅地处理错误条件,例如“艺术家 - 专辑”目录出现在 DIRECTORY01 但不在 DIRECTORY02 中,或者“专辑 - 艺术家”目录的拼写差异。

    • 2
  3. heynnema
    2017-04-04T08:53:05+08:002017-04-04T08:53:05+08:00

    您可以尝试使用meld. 描述是:

    Meld 是 GNOME 桌面的图形差异查看器和合并应用程序。它支持 2 和 3 文件差异、递归目录差异、版本控制下的目录差异(Bazaar、Codeville、CVS、Darcs、Fossil SCM、Git、Mercurial、Monotone、Subversion),以及手动和自动合并的能力文件差异。

    1) 网站是:

    http://meldmerge.org/

    2)如果它在您的存储库列表中,您可以安装它:

    sudo apt-get update
    sudo apt-get install meld
    

    3) 它也在 Ubuntu 软件应用程序中。

    • 0

相关问题

  • 如何通过键盘在 Nautilus 树视图中自动展开所有子目录级别?

  • VirtualBox:Ubuntu 主机和 Ubuntu 来宾。我在来宾中的共享文件夹在哪里?

  • 如果我删除主文件夹中的默认文件夹会怎样?

  • 如何为我的罗技网络摄像头创建 udev 规则 [关闭]

  • 如何在不更改名称的情况下隐藏目录或文件?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve