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条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试