0

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

SaiKiran
  • 6,244
  • 11
  • 43
  • 76
  • Did you achieved this with a bash script? why you dont execute that script from php, with the folder as a parameter? – AFR Feb 23 '17 at 09:47

2 Answers2

1

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.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • Error log `mv: cannot move '/home/Testing/results.json' to '7d3a4c935678a2233222b370655a6c11 /home/Testing/results.json': No such file or directory ` – SaiKiran Feb 23 '17 at 09:53
  • @BackdoorCipher: Can you run it inside the `/home/folder` – Inian Feb 23 '17 at 09:54
  • `while IFS= read -r -d '' file; do echo "$file" "$(md5sum $file)"; done< <(find /home/Testing/ -mindepth 1 -maxdepth 1 -type f -print0) /home/Testing/file.txt d41d8cd98f00b204e9800998ecf8427e /home/Testing/file.txt ` – SaiKiran Feb 23 '17 at 10:00
  • See md5sum gives md5sum along with name.I think there is problem there. – SaiKiran Feb 23 '17 at 10:04
  • @BackdoorCipher : it should work! Can you kindly confirm again? – Inian Feb 23 '17 at 10:45
  • `843617.html nsadmin@research:/media/all_hashes$ md5sum 843429.pdf 6506c1c794fc4fc5d5217155d40e0e3d 843429.pdf nsadmin@research:/media/all_hashes$ locate 6506c1c794fc4fc5d5217155d40e0e3d ls | grep "6506c1c794fc4fc5d5217155d40e0e3d" ` – SaiKiran Feb 23 '17 at 11:01
  • 1
    @BackdoorCipher: It is not possible to understand code in comments, update it as part of the question. – Inian Feb 23 '17 at 11:02
  • @BackdoorCipher: With the `-v` flag in `mv` flag, you should have seen which file is renamed to what, did you see the pdf file you mentioned when you ran the script? – Inian Feb 23 '17 at 11:10
  • Finally : Found my mistake,there is one file with file name as some 100 spaces then _564.doc . I manually removed it – SaiKiran Feb 23 '17 at 11:21
  • @BackdoorCipher: Glad that you found it and fixed it, update in question, that part you claimed that was not working,. – Inian Feb 23 '17 at 11:22
0

Why not use a php script, something like below would work. This would go through all the files, rename them and then if successful delete the old file.

$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); 
}
Sphinx
  • 956
  • 7
  • 21