You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ubuntu中批量重命名目录及子目录内不符合指定sha1sum的mp4文件

实现方案

你提到的基准SHA1值为3c72363260a32992c3ab2e3a5e9b8cf082e02eac,可按以下步骤操作:

步骤1:先执行 dry run 测试,避免误操作

先运行测试命令,只会输出待重命名的文件列表,不会实际修改文件,确认匹配逻辑是否正确:

target_sha1="3c72363260a32992c3ab2e3a5e9b8cf082e02eac"
count=1
find /home/abcd -type f -name "*.mp4" | while read -r file; do
    current_sha1=$(sha1sum "$file" | awk '{print $1}')
    if [ "$current_sha1" != "$target_sha1" ]; then
        echo "待重命名:$file -> vid_${count}.mp4"
        count=$((count+1))
    fi
done
  • 仔细核对输出的列表,确认你需要保留的文件没有被误匹配。

步骤2:执行实际重命名

根据你的存储需求选择对应脚本:

变体1:所有重命名后的文件统一存放至指定目录

target_sha1="3c72363260a32992c3ab2e3a5e9b8cf082e02eac"
count=1
# 自定义输出目录,先创建避免报错
mkdir -p /home/abcd/output
find /home/abcd -type f -name "*.mp4" | while read -r file; do
    current_sha1=$(sha1sum "$file" | awk '{print $1}')
    if [ "$current_sha1" != "$target_sha1" ]; then
        mv "$file" "/home/abcd/output/vid_${count}.mp4"
        count=$((count+1))
    fi
done

变体2:保留原目录结构,仅修改对应目录下的文件名

如果不需要移动文件,只在文件所在目录重命名:

target_sha1="3c72363260a32992c3ab2e3a5e9b8cf082e02eac"
count=1
find /home/abcd -type f -name "*.mp4" | while read -r file; do
    current_sha1=$(sha1sum "$file" | awk '{print $1}')
    if [ "$current_sha1" != "$target_sha1" ]; then
        file_dir=$(dirname "$file")
        mv "$file" "${file_dir}/vid_${count}.mp4"
        count=$((count+1))
    fi
done

注意事项

  • 操作前建议先备份重要视频文件,避免误操作丢失数据
  • 如果文件数量较多,SHA1计算需要一定时间,等待执行完成即可

内容的提问来源于stack exchange,提问作者C0nw0nk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:54:03