查找空文件及其重复项(合作伙伴)

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

我正在尝试训练超正方体。该过程涉及创建三个文件:box 文件、文本文件和图像 (tif) 文件。

创建 .box 文件的工具有时会创建空文件。这些空文件会给引擎带来问题。所以,我想删除空盒子文件以及它们的伙伴。

整个图案如下所示

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

File2.box 是一个空文件(大小为零)。我想找到并删除它及其伙伴(重复项),例如 File2.gt.txt 和 File2.tif。

这可行吗?

bash awk duplicates find
2个回答
3
投票

检查这个简单的脚本,我使用

find
命令搜索所有空
.box
文件 (
-type f -name "*.box" -size 0
),然后使用
.box
标志删除空
-delete
文件,最后删除通过在
.gt.txt
标志内执行
.tif
命令来获取相应的
rm
-exec
文件:

#!/bin/bash

#specifing the directory where the files are located
directory="/path/to/files"

#changing to the specified directory
cd "$directory" || exit

#find and delete empty .box files along with their partners
find . -type f -name "*.box" -size 0 -delete -exec sh -c 'rm -f "${1%.box}.gt.txt" "${1%.box}.tif"' sh {} \;

1
投票

还有另一种方法:

#!/bin/bash

for f in File*.box; do
  [ -s "$f" ] && continue
  base=${f%.*}
  rm ${base}{.gt.txt,.tif,.box}
done

或者,作为一句台词:

for f in File*.box; do [ -s "$f" ] && continue ; rm  ${f%.*}{.gt.txt,.tif,.box}; done
© www.soinside.com 2019 - 2024. All rights reserved.