如果文件缺少重复项(合作伙伴),则删除文件

问题描述 投票:0回答:1

鉴于我在这里发布的问题的成功:查找空文件及其重复项,谢谢 Freeman 和 Mark Setchell,现在鼓励我提出另一个相关问题。在这种情况下,挑战是如果文件缺少合作伙伴,则删除该文件。

Tesseract 中的 text2image 工具有时根本无法生成 .box 文件。

这些文件应该显示为三元组,如下所示:

  • 文件1.box
  • 文件1.gt.txt
  • 文件1.tif
  • 文件2.box
  • 文件2.gt.txt
  • 文件2.tif

但是,当该工具无法生成 box 文件时,我得到的只是两个伙伴文件,如下所示。

  • 文件3.gt.txt
  • 文件3.tif

我想要的是删除那些缺少盒子伙伴的(gt.txt和.tif)文件。

我希望描述清楚。

bash awk find
1个回答
1
投票

这是你想要的吗?

#!/bin/bash

#iterate over the files in the directory
for file in *.{tif,gt.txt}; do
    #get the file name without extension
    file_name="${file%.*}"

    #checking if the corresponding box file exists
    box_file="${file_name}.box"
    if [ ! -f "$box_file" ]; then
        #delete the files without a partner
        rm "$file"
        echo "Deleted $file"
    fi
done
© www.soinside.com 2019 - 2024. All rights reserved.