比较两个文件大小并删除较小的文件

比较两个文件大小并删除较小的文件

我正在寻找一种方法来删除两个文件中较小的一个。我找到了一种方法来查找电影库中的重复文件,但现在我需要删除较小的文件。

bash - 查找所有具有相同名称的文件,无论扩展名如何

当然有一些“陷阱”......列表的当前输出没有扩展名。

例如:

root@fs:ls * |  awk '!/.srt/'  | sed 's/.\{4\}$//' | sort | uniq -d
Captain Fantastic (2016)
The Hurt Locker (2008)
The Warriors (1979)

我需要一种方法来返回并将两个文件与名称进行比较,Captain Fantastic (2016).*因为它们将具有不同的扩展名。 (所有文件都在同一个文件夹中)

这有点超出了问题的范围:

我还想检查FILENAME.srt文件是否存在,如果存在,那么我什么都不做,并将文件名保存到日志中以供手动验证。 (我需要弄清楚 srt 与哪个文件同步)。

答案1

建议:

#!/bin/sh

# Look at filenames in current directory and generate list with filename
# suffixes removed (a filename suffix is anything after the last dot in
# the file name). We assume filenames that does not contain newlines.
# Only unique prefixes will be generated.
for name in ./*; do
        [ ! -f "$name" ] && continue # skip non-regular files
        printf '%s\n' "${name%.*}"
done | sort -u |
while IFS= read -r prefix; do
        # Set the positional parameters to the names matching a particular prefix.
        set -- "$prefix"*

        if [ "$#" -ne 2 ]; then
                printf 'Not exactly two files having prefix "%s"\n' "$prefix" >&2
                continue
        fi

        # Check file sizes and remove smallest.
        if [ "$( stat -c '%s' "$1" )" -lt "$( stat -c '%s' "$2" )" ]; then
                # First file is smaller
                printf 'Would remove "%s"\n' "$1"
                echo rm "$1"
        else
                # Second file is smaller, or same size
                printf 'Would remove "%s"\n' "$2"
                echo rm "$2"
        fi
done

这假设是 GNU stat

答案2

好吧,这确实具体回答了我的问题,在 Re: Kusalananda 的评论中,如果它找到超过 2 个文件,它将把一切搞砸并删除错误的文件。该脚本是根据我的需要量身定制的,但也可以用于其他目的。

#!/bin/bash

#Create Log with Single Entry for Each Duplicate Without File Extension
duplog='dupes.log'
ls * |  awk '!/.srt/'  | sed 's/.\{4\}$//' | sort | uniq -d > "$duplog"

#Testing!
cat "$duplog"

#List Each Iteration of File in log starting with Largest File
log='tmp.log'
while read p; do

#More Testing!
du -k "$p".*

ls -1S  "$p".* >> "$log"
done < $duplog

#Testing!
cat "$log"

#Remove Large File Entry via Sed
#Note: This relies on only two variations being found or it will delete wrong lines in file
sed -i '1~2d' "$log"

#Testing!
cat "$log"

#Delete Smaller File
while read p; do
  echo "Deleting $p"
  rm "$p"
done <"$log"

#Delete Log
rm "$log"

输出:

root@fs:/Movies# du -k tk.m*
4       tk.mkv
0       tk.mp4
root@fs:/Movies# ./test.sh
tk
4       tk.mkv
0       tk.mp4
tk.mkv
tk.mp4
tk.mp4
Deleting tk.mp4
root@fs:/Movies#

PS:我确信这是“hackish”,但它适合我的需要,也是学习过程中的另一个步骤:)

相关内容