Rename files based on checksum





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







6















I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.



Example of the list:



d4cd401ade018617629b39efed7b7be4  foo.bar
8fdb07ca55c164e0d5a69eff49fe800e bar.foo
8b167d01009f066aaf2d6c1ba336d842 foobar


Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.



How I can do that?










share|improve this question





























    6















    I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.



    Example of the list:



    d4cd401ade018617629b39efed7b7be4  foo.bar
    8fdb07ca55c164e0d5a69eff49fe800e bar.foo
    8b167d01009f066aaf2d6c1ba336d842 foobar


    Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.



    How I can do that?










    share|improve this question

























      6












      6








      6


      2






      I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.



      Example of the list:



      d4cd401ade018617629b39efed7b7be4  foo.bar
      8fdb07ca55c164e0d5a69eff49fe800e bar.foo
      8b167d01009f066aaf2d6c1ba336d842 foobar


      Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.



      How I can do that?










      share|improve this question














      I have a md5sum list and a lot of file which I wanted to checksum and then rename them according to the md5sum list.



      Example of the list:



      d4cd401ade018617629b39efed7b7be4  foo.bar
      8fdb07ca55c164e0d5a69eff49fe800e bar.foo
      8b167d01009f066aaf2d6c1ba336d842 foobar


      Now I wanted to checksum every files in current directory, if the checksum are matched with the list above then rename it as the right colum.



      How I can do that?







      files rename hashsum checksum






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 6 '16 at 12:56









      SandPoxSandPox

      377




      377






















          4 Answers
          4






          active

          oldest

          votes


















          2














          I haven't fully tested, it's just theoretically working. Substitute where needed:



          #! /bin/bash
          for II in *
          do
          if [ -f "$II" ]; then
          TMPV=$(md5sum "$II")
          MD="${TMPV% *}"
          TMPV=$(grep "$MD" hashes.txt)
          if [ ! -z "$TMPV" ]; then
          FN="${TMPV#* }"
          echo "Found: $II"
          echo "MD5 is: $MD"
          echo "Which matches $FN in hashes database"
          echo "Will Rename $II TO $FN"
          echo ""
          # CAREFUL, RENAME CMD: mv "$II" "$FN"
          fi;
          fi;
          done;


          As I say, haven't tested it, but it seemed to work on my box.






          share|improve this answer

































            3














            First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.



            Let's say you have the file with the checksum and filenames called filelist.txt then you could use something like:



            while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt





            share|improve this answer
























            • I got error "md5sum: file*: No such file or directory"

              – SandPox
              Dec 7 '16 at 10:47






            • 1





              You got the error because file* does not exist. Change file* to * and it should work. The latter will loop through all files within the directory.

              – Valentin Bajrami
              Dec 7 '16 at 11:11





















            0














            My idea:




            1. At first you need to sort your known checksums: sort checksums.txt > sorted_checksums.txt

            2. Generate file for all existing files and also sort them: md5sum * | sort > real_checksums.txt

            3. Join this two files and exclude records with same new and old names: join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt

            4. Rename all files: cat rename_pairs.txt | xargs -L 1 echo mv (Remove echo from xargs to actually rename files)


            WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt to check that and if there will be any line printed, then you need to use something else (may be simple perl or python program) for steps 3 and 4.






            share|improve this answer































              0














              Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.



              #!/bin/bash
              mkdir renamed
              typeset -A names
              while read -r sum name; do
              names[$sum]=$name
              done <list.md5sum
              for file in *; do
              if [[ -f $file ]]; then
              sum=$(md5sum <"$file"); sum=${sum%% *}
              if [[ -n ${names[$sum]} ]]; then
              mv -- "$file" "renamed/${names[$sum]}"
              fi
              fi
              done





              share|improve this answer
























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "106"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f328421%2frename-files-based-on-checksum%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                I haven't fully tested, it's just theoretically working. Substitute where needed:



                #! /bin/bash
                for II in *
                do
                if [ -f "$II" ]; then
                TMPV=$(md5sum "$II")
                MD="${TMPV% *}"
                TMPV=$(grep "$MD" hashes.txt)
                if [ ! -z "$TMPV" ]; then
                FN="${TMPV#* }"
                echo "Found: $II"
                echo "MD5 is: $MD"
                echo "Which matches $FN in hashes database"
                echo "Will Rename $II TO $FN"
                echo ""
                # CAREFUL, RENAME CMD: mv "$II" "$FN"
                fi;
                fi;
                done;


                As I say, haven't tested it, but it seemed to work on my box.






                share|improve this answer






























                  2














                  I haven't fully tested, it's just theoretically working. Substitute where needed:



                  #! /bin/bash
                  for II in *
                  do
                  if [ -f "$II" ]; then
                  TMPV=$(md5sum "$II")
                  MD="${TMPV% *}"
                  TMPV=$(grep "$MD" hashes.txt)
                  if [ ! -z "$TMPV" ]; then
                  FN="${TMPV#* }"
                  echo "Found: $II"
                  echo "MD5 is: $MD"
                  echo "Which matches $FN in hashes database"
                  echo "Will Rename $II TO $FN"
                  echo ""
                  # CAREFUL, RENAME CMD: mv "$II" "$FN"
                  fi;
                  fi;
                  done;


                  As I say, haven't tested it, but it seemed to work on my box.






                  share|improve this answer




























                    2












                    2








                    2







                    I haven't fully tested, it's just theoretically working. Substitute where needed:



                    #! /bin/bash
                    for II in *
                    do
                    if [ -f "$II" ]; then
                    TMPV=$(md5sum "$II")
                    MD="${TMPV% *}"
                    TMPV=$(grep "$MD" hashes.txt)
                    if [ ! -z "$TMPV" ]; then
                    FN="${TMPV#* }"
                    echo "Found: $II"
                    echo "MD5 is: $MD"
                    echo "Which matches $FN in hashes database"
                    echo "Will Rename $II TO $FN"
                    echo ""
                    # CAREFUL, RENAME CMD: mv "$II" "$FN"
                    fi;
                    fi;
                    done;


                    As I say, haven't tested it, but it seemed to work on my box.






                    share|improve this answer















                    I haven't fully tested, it's just theoretically working. Substitute where needed:



                    #! /bin/bash
                    for II in *
                    do
                    if [ -f "$II" ]; then
                    TMPV=$(md5sum "$II")
                    MD="${TMPV% *}"
                    TMPV=$(grep "$MD" hashes.txt)
                    if [ ! -z "$TMPV" ]; then
                    FN="${TMPV#* }"
                    echo "Found: $II"
                    echo "MD5 is: $MD"
                    echo "Which matches $FN in hashes database"
                    echo "Will Rename $II TO $FN"
                    echo ""
                    # CAREFUL, RENAME CMD: mv "$II" "$FN"
                    fi;
                    fi;
                    done;


                    As I say, haven't tested it, but it seemed to work on my box.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 4 hours ago









                    Rui F Ribeiro

                    41.9k1483142




                    41.9k1483142










                    answered Dec 6 '16 at 13:46









                    nonzyrononzyro

                    808




                    808

























                        3














                        First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.



                        Let's say you have the file with the checksum and filenames called filelist.txt then you could use something like:



                        while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt





                        share|improve this answer
























                        • I got error "md5sum: file*: No such file or directory"

                          – SandPox
                          Dec 7 '16 at 10:47






                        • 1





                          You got the error because file* does not exist. Change file* to * and it should work. The latter will loop through all files within the directory.

                          – Valentin Bajrami
                          Dec 7 '16 at 11:11


















                        3














                        First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.



                        Let's say you have the file with the checksum and filenames called filelist.txt then you could use something like:



                        while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt





                        share|improve this answer
























                        • I got error "md5sum: file*: No such file or directory"

                          – SandPox
                          Dec 7 '16 at 10:47






                        • 1





                          You got the error because file* does not exist. Change file* to * and it should work. The latter will loop through all files within the directory.

                          – Valentin Bajrami
                          Dec 7 '16 at 11:11
















                        3












                        3








                        3







                        First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.



                        Let's say you have the file with the checksum and filenames called filelist.txt then you could use something like:



                        while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt





                        share|improve this answer













                        First of all, I'm not going to claim that this is the most profound solution but, here is one way to do it.



                        Let's say you have the file with the checksum and filenames called filelist.txt then you could use something like:



                        while read -r checksum fname; do for f in file*; do if [[ $checksum == $(md5sum "$f" | cut -d' ' -f1) ]]; then mv "$f" "$fname"; fi ; done ; done < filelist.txt






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 6 '16 at 13:45









                        Valentin BajramiValentin Bajrami

                        6,17611729




                        6,17611729













                        • I got error "md5sum: file*: No such file or directory"

                          – SandPox
                          Dec 7 '16 at 10:47






                        • 1





                          You got the error because file* does not exist. Change file* to * and it should work. The latter will loop through all files within the directory.

                          – Valentin Bajrami
                          Dec 7 '16 at 11:11





















                        • I got error "md5sum: file*: No such file or directory"

                          – SandPox
                          Dec 7 '16 at 10:47






                        • 1





                          You got the error because file* does not exist. Change file* to * and it should work. The latter will loop through all files within the directory.

                          – Valentin Bajrami
                          Dec 7 '16 at 11:11



















                        I got error "md5sum: file*: No such file or directory"

                        – SandPox
                        Dec 7 '16 at 10:47





                        I got error "md5sum: file*: No such file or directory"

                        – SandPox
                        Dec 7 '16 at 10:47




                        1




                        1





                        You got the error because file* does not exist. Change file* to * and it should work. The latter will loop through all files within the directory.

                        – Valentin Bajrami
                        Dec 7 '16 at 11:11







                        You got the error because file* does not exist. Change file* to * and it should work. The latter will loop through all files within the directory.

                        – Valentin Bajrami
                        Dec 7 '16 at 11:11













                        0














                        My idea:




                        1. At first you need to sort your known checksums: sort checksums.txt > sorted_checksums.txt

                        2. Generate file for all existing files and also sort them: md5sum * | sort > real_checksums.txt

                        3. Join this two files and exclude records with same new and old names: join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt

                        4. Rename all files: cat rename_pairs.txt | xargs -L 1 echo mv (Remove echo from xargs to actually rename files)


                        WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt to check that and if there will be any line printed, then you need to use something else (may be simple perl or python program) for steps 3 and 4.






                        share|improve this answer




























                          0














                          My idea:




                          1. At first you need to sort your known checksums: sort checksums.txt > sorted_checksums.txt

                          2. Generate file for all existing files and also sort them: md5sum * | sort > real_checksums.txt

                          3. Join this two files and exclude records with same new and old names: join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt

                          4. Rename all files: cat rename_pairs.txt | xargs -L 1 echo mv (Remove echo from xargs to actually rename files)


                          WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt to check that and if there will be any line printed, then you need to use something else (may be simple perl or python program) for steps 3 and 4.






                          share|improve this answer


























                            0












                            0








                            0







                            My idea:




                            1. At first you need to sort your known checksums: sort checksums.txt > sorted_checksums.txt

                            2. Generate file for all existing files and also sort them: md5sum * | sort > real_checksums.txt

                            3. Join this two files and exclude records with same new and old names: join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt

                            4. Rename all files: cat rename_pairs.txt | xargs -L 1 echo mv (Remove echo from xargs to actually rename files)


                            WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt to check that and if there will be any line printed, then you need to use something else (may be simple perl or python program) for steps 3 and 4.






                            share|improve this answer













                            My idea:




                            1. At first you need to sort your known checksums: sort checksums.txt > sorted_checksums.txt

                            2. Generate file for all existing files and also sort them: md5sum * | sort > real_checksums.txt

                            3. Join this two files and exclude records with same new and old names: join -o "2.2 1.2" sorted_checksums.txt real_checksums.txt | awk '$1 != $2' > rename_pairs.txt

                            4. Rename all files: cat rename_pairs.txt | xargs -L 1 echo mv (Remove echo from xargs to actually rename files)


                            WARNING: this will work only if there is no spaces in filenames. You could use awk 'NF != 2' sorted_checksums.txt real_checksums.txt to check that and if there will be any line printed, then you need to use something else (may be simple perl or python program) for steps 3 and 4.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 6 '16 at 16:36









                            Fedor DikarevFedor Dikarev

                            1,103310




                            1,103310























                                0














                                Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.



                                #!/bin/bash
                                mkdir renamed
                                typeset -A names
                                while read -r sum name; do
                                names[$sum]=$name
                                done <list.md5sum
                                for file in *; do
                                if [[ -f $file ]]; then
                                sum=$(md5sum <"$file"); sum=${sum%% *}
                                if [[ -n ${names[$sum]} ]]; then
                                mv -- "$file" "renamed/${names[$sum]}"
                                fi
                                fi
                                done





                                share|improve this answer




























                                  0














                                  Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.



                                  #!/bin/bash
                                  mkdir renamed
                                  typeset -A names
                                  while read -r sum name; do
                                  names[$sum]=$name
                                  done <list.md5sum
                                  for file in *; do
                                  if [[ -f $file ]]; then
                                  sum=$(md5sum <"$file"); sum=${sum%% *}
                                  if [[ -n ${names[$sum]} ]]; then
                                  mv -- "$file" "renamed/${names[$sum]}"
                                  fi
                                  fi
                                  done





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.



                                    #!/bin/bash
                                    mkdir renamed
                                    typeset -A names
                                    while read -r sum name; do
                                    names[$sum]=$name
                                    done <list.md5sum
                                    for file in *; do
                                    if [[ -f $file ]]; then
                                    sum=$(md5sum <"$file"); sum=${sum%% *}
                                    if [[ -n ${names[$sum]} ]]; then
                                    mv -- "$file" "renamed/${names[$sum]}"
                                    fi
                                    fi
                                    done





                                    share|improve this answer













                                    Read the checksums into an associative array, then walk through the files and rename them as needed. Put the renamed files in a separate directory tree, in case there is overlap between the new names and the old names.



                                    #!/bin/bash
                                    mkdir renamed
                                    typeset -A names
                                    while read -r sum name; do
                                    names[$sum]=$name
                                    done <list.md5sum
                                    for file in *; do
                                    if [[ -f $file ]]; then
                                    sum=$(md5sum <"$file"); sum=${sum%% *}
                                    if [[ -n ${names[$sum]} ]]; then
                                    mv -- "$file" "renamed/${names[$sum]}"
                                    fi
                                    fi
                                    done






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 7 '16 at 1:31









                                    GillesGilles

                                    546k12911101624




                                    546k12911101624






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f328421%2frename-files-based-on-checksum%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        濃尾地震

                                        How to rewrite equation of hyperbola in standard form

                                        No ethernet ip address in my vocore2