du20150401 2017-02-23 09:44
浏览 112
已采纳

如何将文件夹中的文件重命名为相应的md5sum?

I have large samples in a folder with plain names and file names with spaces also and i want to renames all the files to its corresponding md5sum.

I tried this logic for f in $(find /home/SomeFolder/ -type f) ;do mv "$f" "$(md5sum $f)";done

But this is not working properly with some error like mv: cannot move to indicating no such directory.

Also i tried this logic Rename files to md5 sum + extension (BASH) and tried this for f in $(find /home/Testing/ -type f) ;do echomd5sum $f;mv $f /home/Testing/"echomd5sum $f``"; done; ` But it is not working.

Any suggestions to solve this.

I want to replace a file to its md5sum name without any extension

sample.zip --> c75b5e2ca63adb462f4bb941e0c9f509

c75b5e2ca63adb462f4bb941e0c9f509c75b5e2ca63adb462f --> c75b5e2ca63adb462f4bb941e0c9f509

file name with spaces.php --> a75b5e2ca63adb462f4bb941e0c9f509

  • 写回答

2条回答 默认 最新

  • dongpaocuan7498 2017-02-23 09:49
    关注

    See Why you shouldn't parse the output of ls or find in a for-loop, ParsingLs,

    If you have file names with spaces also recommend using -print0 option of GNU findutils for the job which embeds a \0 character after file name with read with a null delimiter as below.

    Run the script below inside /home/SomeFolder and use find from the current directory as

    #!/bin/bash
    
    while IFS= read -r -d '' file
    do
        mv -v "$file" "$(md5sum $file | cut -d ' ' -f 1)"
    done< <(find . -mindepth 1 -maxdepth 1 -type f -print0)
    

    The depth options ensure the current folder . is not included in the search result. This will now get all the files in your current directory ( remember it does not recurse through sub-directories) and renames your file with the md5sum of the filenames.

    The -v flag in mv is for verbose output ( which you can remove) for seeing how the files are to be renamed as.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度