可以通过以下步骤实现:
find /path/to/directory -type f -exec md5sum {} + | sort | uniq -d -w 32
这个命令会在指定目录下递归地查找所有文件,并计算每个文件的MD5哈希值。然后,通过排序和去重,找到重复的文件。其中,/path/to/directory
是你要查找的目录路径。
#!/bin/bash
# 指定要查找的目录
directory="/path/to/directory"
# 查找重复文件
duplicates=$(find "$directory" -type f -exec md5sum {} + | sort | uniq -d -w 32)
# 遍历重复文件列表
while IFS= read -r line; do
# 提取文件路径和文件名
file=$(echo "$line" | awk '{print $2}')
filename=$(basename "$file")
# 删除重复文件
rm "$file"
echo "已删除文件: $filename"
done <<< "$duplicates"
将以上脚本保存为一个.sh
文件,并将/path/to/directory
替换为你要查找的目录路径。然后,通过运行该脚本,它将逐个删除重复的文件,并输出已删除的文件名。
这个脚本的原理是先使用find
命令找到重复的文件,然后使用循环结构遍历重复文件列表,并使用rm
命令删除每个重复文件。
这种方法可以帮助你快速删除重复的文件,提升存储空间利用率。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云