2017-02-23 23 views
0

我在plain names and file names with spaces also文件夹中有大样本,我想将所有文件重命名为其对应的md5sum。如何将文件夹内的文件重命名为其对应的md5sum?

我想这个逻辑for f in $(find /home/SomeFolder/ -type f) ;do mv "$f" "$(md5sum $f)";done

但这不是一些错误正常工作像mv: cannot move to表示没有对应的目录。

而且我想这个逻辑Rename files to md5 sum + extension (BASH)并试用了此for f in $(find /home/Testing/ -type f) ;do echo的md5sum $ F ;mv $f /home/Testing/"echo的md5sum $ f``“;完成。 ` 但它不工作

任何建议,以解决这一

。我要替换文件到其的md5sum名称没有任何扩展

sample.zip --> c75b5e2ca63adb462f4bb941e0c9f509

c75b5e2ca63adb462f4bb941e0c9f509c75b5e2ca63adb462f --> c75b5e2ca63adb462f4bb941e0c9f509

file name with spaces.php --> a75b5e2ca63adb462f4bb941e0c9f509

+0

你用bash脚本实现了这个吗?为什么你不执行该脚本从PHP,文件夹作为参数? – AFR

回答

1

明白为什么你不应该分析的lsfind输出在for循环,ParsingLs

如果你有file names with spaces also建议使用-print0选项GNU findutilsread文件名后嵌入\0字符的作业,其空分隔符如下所示。

运行里面/home/SomeFolder下面的脚本,并使用从当前目录中找到作为

#!/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) 

深度选项确保当前文件夹是不包含在搜索结果.。这将获得当前目录中的所有文件(请记住,它不会递归通过子目录)并使用文件名称的md5sum重命名文件。

mv中的-v标志用于详细输出(可以删除)以查看文件如何重命名为。

+0

错误日志'mv:不能移动'/home/Testing/results.json'到'7d3a4c935678a2233222b370655a6c11 /home/Testing/results.json':没有这样的文件或目录 ' –

+0

@BackdoorCipher:你可以在/ home/folder' – Inian

+0

'while IFS = read -r -d''file;做echo“$ file”“$(md5sum $ file)”;完成<(find/home/Testing/-mindepth 1 -maxdepth 1 -type f -print0) /home/Testing/file.txt d41d8cd98f00b204e9800998ecf8427e /home/Testing/file.txt ' –

0

为什么不使用一个PHP脚本,像下面的东西会工作。这将通过所有文件,重命名它们,然后如果成功删除旧文件。

$path = ''; 
if ($handle = opendir($path)) { 
    while (false !== ($file = readdir($handle))) { 
     if (substr($file, 0, 1) == '.') { 
      continue; 
     } 

      if (rename($path . $file, $path . md5($file))) 
      { 
       unlink($path . $file); 
      } 

    } 
    closedir($handle); 
} 
相关问题