Generate MD5sum for all files in a directory, and then write (filename).md5 for each file containing that file's MD5SUM

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a directory full of files. Each file will be copied to a specific type of destination host.



I want to calculate an MD5 sum for each file in the directory, and store that md5 sum in a file that matches the name of the file that generated the sum, but with .md5 appended.



So, for instance, if I have a directory with:



a.bin
b.bin
c.bin


The final result should be:



a.bin
a.bin.md5 # a.bin's calculated checksum
b.bin
b.bin.md5 # b.bin's calculated checksum
c.bin
c.bin.md5 # c.bin's calculated checksum


I have attempted this with find exec, and with xargs.



With find, I tried this command:



find . -type f -exec md5sum + > .md5


Using xargs, I tried this command:



find . -type f | xargs -I md5sum > .md5


In either case, I end up with a file called .txt, which isn't really what I am looking for.



Could anyone point out how to tweak these to generate the md5 files I am looking to generate?







share|improve this question
















  • 1




    Dumping all the output to a single file will let you use md5sum -c later to verify all files at once.
    – Ignacio Vazquez-Abrams
    Nov 6 '17 at 20:21










  • The problem is we won't be copying all files to the destinations. Only one file will be copied. So, we'd end up with a monolithic file listing files that mostly aren't present on the destination hosts. Using the example above, some hosts get a.bin, some get b.bin. The a.bin hosts don't need md5sum info for b.bin and c.bin. I hope that makes sense. :)
    – Matthew
    Nov 6 '17 at 20:38














up vote
1
down vote

favorite












I have a directory full of files. Each file will be copied to a specific type of destination host.



I want to calculate an MD5 sum for each file in the directory, and store that md5 sum in a file that matches the name of the file that generated the sum, but with .md5 appended.



So, for instance, if I have a directory with:



a.bin
b.bin
c.bin


The final result should be:



a.bin
a.bin.md5 # a.bin's calculated checksum
b.bin
b.bin.md5 # b.bin's calculated checksum
c.bin
c.bin.md5 # c.bin's calculated checksum


I have attempted this with find exec, and with xargs.



With find, I tried this command:



find . -type f -exec md5sum + > .md5


Using xargs, I tried this command:



find . -type f | xargs -I md5sum > .md5


In either case, I end up with a file called .txt, which isn't really what I am looking for.



Could anyone point out how to tweak these to generate the md5 files I am looking to generate?







share|improve this question
















  • 1




    Dumping all the output to a single file will let you use md5sum -c later to verify all files at once.
    – Ignacio Vazquez-Abrams
    Nov 6 '17 at 20:21










  • The problem is we won't be copying all files to the destinations. Only one file will be copied. So, we'd end up with a monolithic file listing files that mostly aren't present on the destination hosts. Using the example above, some hosts get a.bin, some get b.bin. The a.bin hosts don't need md5sum info for b.bin and c.bin. I hope that makes sense. :)
    – Matthew
    Nov 6 '17 at 20:38












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a directory full of files. Each file will be copied to a specific type of destination host.



I want to calculate an MD5 sum for each file in the directory, and store that md5 sum in a file that matches the name of the file that generated the sum, but with .md5 appended.



So, for instance, if I have a directory with:



a.bin
b.bin
c.bin


The final result should be:



a.bin
a.bin.md5 # a.bin's calculated checksum
b.bin
b.bin.md5 # b.bin's calculated checksum
c.bin
c.bin.md5 # c.bin's calculated checksum


I have attempted this with find exec, and with xargs.



With find, I tried this command:



find . -type f -exec md5sum + > .md5


Using xargs, I tried this command:



find . -type f | xargs -I md5sum > .md5


In either case, I end up with a file called .txt, which isn't really what I am looking for.



Could anyone point out how to tweak these to generate the md5 files I am looking to generate?







share|improve this question












I have a directory full of files. Each file will be copied to a specific type of destination host.



I want to calculate an MD5 sum for each file in the directory, and store that md5 sum in a file that matches the name of the file that generated the sum, but with .md5 appended.



So, for instance, if I have a directory with:



a.bin
b.bin
c.bin


The final result should be:



a.bin
a.bin.md5 # a.bin's calculated checksum
b.bin
b.bin.md5 # b.bin's calculated checksum
c.bin
c.bin.md5 # c.bin's calculated checksum


I have attempted this with find exec, and with xargs.



With find, I tried this command:



find . -type f -exec md5sum + > .md5


Using xargs, I tried this command:



find . -type f | xargs -I md5sum > .md5


In either case, I end up with a file called .txt, which isn't really what I am looking for.



Could anyone point out how to tweak these to generate the md5 files I am looking to generate?









share|improve this question











share|improve this question




share|improve this question










asked Nov 6 '17 at 20:10









Matthew

1165




1165







  • 1




    Dumping all the output to a single file will let you use md5sum -c later to verify all files at once.
    – Ignacio Vazquez-Abrams
    Nov 6 '17 at 20:21










  • The problem is we won't be copying all files to the destinations. Only one file will be copied. So, we'd end up with a monolithic file listing files that mostly aren't present on the destination hosts. Using the example above, some hosts get a.bin, some get b.bin. The a.bin hosts don't need md5sum info for b.bin and c.bin. I hope that makes sense. :)
    – Matthew
    Nov 6 '17 at 20:38












  • 1




    Dumping all the output to a single file will let you use md5sum -c later to verify all files at once.
    – Ignacio Vazquez-Abrams
    Nov 6 '17 at 20:21










  • The problem is we won't be copying all files to the destinations. Only one file will be copied. So, we'd end up with a monolithic file listing files that mostly aren't present on the destination hosts. Using the example above, some hosts get a.bin, some get b.bin. The a.bin hosts don't need md5sum info for b.bin and c.bin. I hope that makes sense. :)
    – Matthew
    Nov 6 '17 at 20:38







1




1




Dumping all the output to a single file will let you use md5sum -c later to verify all files at once.
– Ignacio Vazquez-Abrams
Nov 6 '17 at 20:21




Dumping all the output to a single file will let you use md5sum -c later to verify all files at once.
– Ignacio Vazquez-Abrams
Nov 6 '17 at 20:21












The problem is we won't be copying all files to the destinations. Only one file will be copied. So, we'd end up with a monolithic file listing files that mostly aren't present on the destination hosts. Using the example above, some hosts get a.bin, some get b.bin. The a.bin hosts don't need md5sum info for b.bin and c.bin. I hope that makes sense. :)
– Matthew
Nov 6 '17 at 20:38




The problem is we won't be copying all files to the destinations. Only one file will be copied. So, we'd end up with a monolithic file listing files that mostly aren't present on the destination hosts. Using the example above, some hosts get a.bin, some get b.bin. The a.bin hosts don't need md5sum info for b.bin and c.bin. I hope that makes sense. :)
– Matthew
Nov 6 '17 at 20:38










5 Answers
5






active

oldest

votes

















up vote
2
down vote



accepted










cd /path/to/files &&
for file in *; do
if [[ -f "$file" ]]; then
md5sum -- "$file" > "$file.md5"
fi
done





share|improve this answer






















  • Works perfectly, thank you!
    – Matthew
    Nov 6 '17 at 20:19

















up vote
2
down vote













Redirection is an operator of the shell.



In:



find some args > .md5


find is run with some args and its stdout redirected to .md5.



Here, you'd need find to start a shell to perform one redirection for each file:



find . -type f -exec sh -c '
for file do
md5sum "$file" > "$file.md5"
done' sh +


If you want to avoid the directory component in the .md5 part, you can either use the -execdir alternatives supported by many find implementations (though with some you'll still get a ./ directory component, and with some, that will still run one shell per file even with +:



find . -type f -execdir sh -c '
for file do
md5sum "$file" > "$file.md5"
done' sh +


Or you can do it as:



find . -type f -exec sh -c '
for file do
(cd "${file%/*" && exec md5sum -- "$file##*/") > "$file.md5"
done' sh +





share|improve this answer





























    up vote
    0
    down vote













    I know there are side effects to piping into a while loop, but I like the simplicilty, readability and extensibilty of this solution



    find . -type f ! -name *.md5 |
    while read a; do md5sum $a > $a.md5; done


    To use this scipplet to check sums simply:



    find . -type f ! -name *.md5 |
    while read a; do echo $a; md5sum -c $a.md5; done





    share|improve this answer



























      up vote
      -1
      down vote













      find . -name '*.iso' -type f -exec sh -c "ls -lh ; pv | md5sum > .md5" ;


      optional



      ls -lh ;


      to show processing file, and



      pv 


      to show progressbar on big (slow) iso file in my case






      share|improve this answer




















      • Looks OK, but you're restricting it to .iso files.
        – telcoM
        Jan 20 at 17:50

















      up vote
      -1
      down vote













      cd /path/to/files && ls -1| xargs md5sum | xargs -l bash -c 'echo $0" "$1 > $1.md5'


      first xargs generate , second xargs picks up and save to .md5



      If you need filter file patter, you can replace ls with find or add grep afterwards.






      share|improve this answer






















      • Parses output of ls and uses unquoted variable expansions.
        – Kusalananda
        Feb 1 at 17:43










      • Can you explain your comments? you can replace ls with find or any other command.
        – Ming Jia
        Feb 1 at 19:12










      • Assume spaces in any of the names in the files directory, for example. Also consider what would happen if one of the names is a directory.
        – Kusalananda
        Feb 2 at 6:33











      • @MingJia Parsing output of ls is considered generally bad practice (see this ). Even if there aren't any difficult filenames, that doesn't mean one shouldn't anticipate them. Second big problem is the $0 and $1 should be double-quoted. Because of "word splitting", if you have file with spaces or tabs in filename, ` > $1` will have ambiguous redirect error. Try var="with space.txt"; echo "hi" > $var and then var="with space.txt"; echo "hi" > "$var"
        – Sergiy Kolodyazhnyy
        Feb 2 at 6:55










      • @MingJia Don't get discouraged, you'll learn to pick up on these things if you stick around on the site, with time of course. Welcome, to the site and consider re-working your answer.
        – Sergiy Kolodyazhnyy
        Feb 2 at 6:57










      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f402907%2fgenerate-md5sum-for-all-files-in-a-directory-and-then-write-filename-md5-for%23new-answer', 'question_page');

      );

      Post as a guest






























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      cd /path/to/files &&
      for file in *; do
      if [[ -f "$file" ]]; then
      md5sum -- "$file" > "$file.md5"
      fi
      done





      share|improve this answer






















      • Works perfectly, thank you!
        – Matthew
        Nov 6 '17 at 20:19














      up vote
      2
      down vote



      accepted










      cd /path/to/files &&
      for file in *; do
      if [[ -f "$file" ]]; then
      md5sum -- "$file" > "$file.md5"
      fi
      done





      share|improve this answer






















      • Works perfectly, thank you!
        – Matthew
        Nov 6 '17 at 20:19












      up vote
      2
      down vote



      accepted







      up vote
      2
      down vote



      accepted






      cd /path/to/files &&
      for file in *; do
      if [[ -f "$file" ]]; then
      md5sum -- "$file" > "$file.md5"
      fi
      done





      share|improve this answer














      cd /path/to/files &&
      for file in *; do
      if [[ -f "$file" ]]; then
      md5sum -- "$file" > "$file.md5"
      fi
      done






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 7 '17 at 9:04









      Stéphane Chazelas

      283k53521854




      283k53521854










      answered Nov 6 '17 at 20:13









      DopeGhoti

      40.6k54979




      40.6k54979











      • Works perfectly, thank you!
        – Matthew
        Nov 6 '17 at 20:19
















      • Works perfectly, thank you!
        – Matthew
        Nov 6 '17 at 20:19















      Works perfectly, thank you!
      – Matthew
      Nov 6 '17 at 20:19




      Works perfectly, thank you!
      – Matthew
      Nov 6 '17 at 20:19












      up vote
      2
      down vote













      Redirection is an operator of the shell.



      In:



      find some args > .md5


      find is run with some args and its stdout redirected to .md5.



      Here, you'd need find to start a shell to perform one redirection for each file:



      find . -type f -exec sh -c '
      for file do
      md5sum "$file" > "$file.md5"
      done' sh +


      If you want to avoid the directory component in the .md5 part, you can either use the -execdir alternatives supported by many find implementations (though with some you'll still get a ./ directory component, and with some, that will still run one shell per file even with +:



      find . -type f -execdir sh -c '
      for file do
      md5sum "$file" > "$file.md5"
      done' sh +


      Or you can do it as:



      find . -type f -exec sh -c '
      for file do
      (cd "${file%/*" && exec md5sum -- "$file##*/") > "$file.md5"
      done' sh +





      share|improve this answer


























        up vote
        2
        down vote













        Redirection is an operator of the shell.



        In:



        find some args > .md5


        find is run with some args and its stdout redirected to .md5.



        Here, you'd need find to start a shell to perform one redirection for each file:



        find . -type f -exec sh -c '
        for file do
        md5sum "$file" > "$file.md5"
        done' sh +


        If you want to avoid the directory component in the .md5 part, you can either use the -execdir alternatives supported by many find implementations (though with some you'll still get a ./ directory component, and with some, that will still run one shell per file even with +:



        find . -type f -execdir sh -c '
        for file do
        md5sum "$file" > "$file.md5"
        done' sh +


        Or you can do it as:



        find . -type f -exec sh -c '
        for file do
        (cd "${file%/*" && exec md5sum -- "$file##*/") > "$file.md5"
        done' sh +





        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          Redirection is an operator of the shell.



          In:



          find some args > .md5


          find is run with some args and its stdout redirected to .md5.



          Here, you'd need find to start a shell to perform one redirection for each file:



          find . -type f -exec sh -c '
          for file do
          md5sum "$file" > "$file.md5"
          done' sh +


          If you want to avoid the directory component in the .md5 part, you can either use the -execdir alternatives supported by many find implementations (though with some you'll still get a ./ directory component, and with some, that will still run one shell per file even with +:



          find . -type f -execdir sh -c '
          for file do
          md5sum "$file" > "$file.md5"
          done' sh +


          Or you can do it as:



          find . -type f -exec sh -c '
          for file do
          (cd "${file%/*" && exec md5sum -- "$file##*/") > "$file.md5"
          done' sh +





          share|improve this answer














          Redirection is an operator of the shell.



          In:



          find some args > .md5


          find is run with some args and its stdout redirected to .md5.



          Here, you'd need find to start a shell to perform one redirection for each file:



          find . -type f -exec sh -c '
          for file do
          md5sum "$file" > "$file.md5"
          done' sh +


          If you want to avoid the directory component in the .md5 part, you can either use the -execdir alternatives supported by many find implementations (though with some you'll still get a ./ directory component, and with some, that will still run one shell per file even with +:



          find . -type f -execdir sh -c '
          for file do
          md5sum "$file" > "$file.md5"
          done' sh +


          Or you can do it as:



          find . -type f -exec sh -c '
          for file do
          (cd "${file%/*" && exec md5sum -- "$file##*/") > "$file.md5"
          done' sh +






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 7 '17 at 9:09

























          answered Nov 6 '17 at 21:36









          Stéphane Chazelas

          283k53521854




          283k53521854




















              up vote
              0
              down vote













              I know there are side effects to piping into a while loop, but I like the simplicilty, readability and extensibilty of this solution



              find . -type f ! -name *.md5 |
              while read a; do md5sum $a > $a.md5; done


              To use this scipplet to check sums simply:



              find . -type f ! -name *.md5 |
              while read a; do echo $a; md5sum -c $a.md5; done





              share|improve this answer
























                up vote
                0
                down vote













                I know there are side effects to piping into a while loop, but I like the simplicilty, readability and extensibilty of this solution



                find . -type f ! -name *.md5 |
                while read a; do md5sum $a > $a.md5; done


                To use this scipplet to check sums simply:



                find . -type f ! -name *.md5 |
                while read a; do echo $a; md5sum -c $a.md5; done





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I know there are side effects to piping into a while loop, but I like the simplicilty, readability and extensibilty of this solution



                  find . -type f ! -name *.md5 |
                  while read a; do md5sum $a > $a.md5; done


                  To use this scipplet to check sums simply:



                  find . -type f ! -name *.md5 |
                  while read a; do echo $a; md5sum -c $a.md5; done





                  share|improve this answer












                  I know there are side effects to piping into a while loop, but I like the simplicilty, readability and extensibilty of this solution



                  find . -type f ! -name *.md5 |
                  while read a; do md5sum $a > $a.md5; done


                  To use this scipplet to check sums simply:



                  find . -type f ! -name *.md5 |
                  while read a; do echo $a; md5sum -c $a.md5; done






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 20 at 18:15









                  Stefan

                  863




                  863




















                      up vote
                      -1
                      down vote













                      find . -name '*.iso' -type f -exec sh -c "ls -lh ; pv | md5sum > .md5" ;


                      optional



                      ls -lh ;


                      to show processing file, and



                      pv 


                      to show progressbar on big (slow) iso file in my case






                      share|improve this answer




















                      • Looks OK, but you're restricting it to .iso files.
                        – telcoM
                        Jan 20 at 17:50














                      up vote
                      -1
                      down vote













                      find . -name '*.iso' -type f -exec sh -c "ls -lh ; pv | md5sum > .md5" ;


                      optional



                      ls -lh ;


                      to show processing file, and



                      pv 


                      to show progressbar on big (slow) iso file in my case






                      share|improve this answer




















                      • Looks OK, but you're restricting it to .iso files.
                        – telcoM
                        Jan 20 at 17:50












                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      find . -name '*.iso' -type f -exec sh -c "ls -lh ; pv | md5sum > .md5" ;


                      optional



                      ls -lh ;


                      to show processing file, and



                      pv 


                      to show progressbar on big (slow) iso file in my case






                      share|improve this answer












                      find . -name '*.iso' -type f -exec sh -c "ls -lh ; pv | md5sum > .md5" ;


                      optional



                      ls -lh ;


                      to show processing file, and



                      pv 


                      to show progressbar on big (slow) iso file in my case







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 20 at 14:34









                      f7r

                      1




                      1











                      • Looks OK, but you're restricting it to .iso files.
                        – telcoM
                        Jan 20 at 17:50
















                      • Looks OK, but you're restricting it to .iso files.
                        – telcoM
                        Jan 20 at 17:50















                      Looks OK, but you're restricting it to .iso files.
                      – telcoM
                      Jan 20 at 17:50




                      Looks OK, but you're restricting it to .iso files.
                      – telcoM
                      Jan 20 at 17:50










                      up vote
                      -1
                      down vote













                      cd /path/to/files && ls -1| xargs md5sum | xargs -l bash -c 'echo $0" "$1 > $1.md5'


                      first xargs generate , second xargs picks up and save to .md5



                      If you need filter file patter, you can replace ls with find or add grep afterwards.






                      share|improve this answer






















                      • Parses output of ls and uses unquoted variable expansions.
                        – Kusalananda
                        Feb 1 at 17:43










                      • Can you explain your comments? you can replace ls with find or any other command.
                        – Ming Jia
                        Feb 1 at 19:12










                      • Assume spaces in any of the names in the files directory, for example. Also consider what would happen if one of the names is a directory.
                        – Kusalananda
                        Feb 2 at 6:33











                      • @MingJia Parsing output of ls is considered generally bad practice (see this ). Even if there aren't any difficult filenames, that doesn't mean one shouldn't anticipate them. Second big problem is the $0 and $1 should be double-quoted. Because of "word splitting", if you have file with spaces or tabs in filename, ` > $1` will have ambiguous redirect error. Try var="with space.txt"; echo "hi" > $var and then var="with space.txt"; echo "hi" > "$var"
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:55










                      • @MingJia Don't get discouraged, you'll learn to pick up on these things if you stick around on the site, with time of course. Welcome, to the site and consider re-working your answer.
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:57














                      up vote
                      -1
                      down vote













                      cd /path/to/files && ls -1| xargs md5sum | xargs -l bash -c 'echo $0" "$1 > $1.md5'


                      first xargs generate , second xargs picks up and save to .md5



                      If you need filter file patter, you can replace ls with find or add grep afterwards.






                      share|improve this answer






















                      • Parses output of ls and uses unquoted variable expansions.
                        – Kusalananda
                        Feb 1 at 17:43










                      • Can you explain your comments? you can replace ls with find or any other command.
                        – Ming Jia
                        Feb 1 at 19:12










                      • Assume spaces in any of the names in the files directory, for example. Also consider what would happen if one of the names is a directory.
                        – Kusalananda
                        Feb 2 at 6:33











                      • @MingJia Parsing output of ls is considered generally bad practice (see this ). Even if there aren't any difficult filenames, that doesn't mean one shouldn't anticipate them. Second big problem is the $0 and $1 should be double-quoted. Because of "word splitting", if you have file with spaces or tabs in filename, ` > $1` will have ambiguous redirect error. Try var="with space.txt"; echo "hi" > $var and then var="with space.txt"; echo "hi" > "$var"
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:55










                      • @MingJia Don't get discouraged, you'll learn to pick up on these things if you stick around on the site, with time of course. Welcome, to the site and consider re-working your answer.
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:57












                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      cd /path/to/files && ls -1| xargs md5sum | xargs -l bash -c 'echo $0" "$1 > $1.md5'


                      first xargs generate , second xargs picks up and save to .md5



                      If you need filter file patter, you can replace ls with find or add grep afterwards.






                      share|improve this answer














                      cd /path/to/files && ls -1| xargs md5sum | xargs -l bash -c 'echo $0" "$1 > $1.md5'


                      first xargs generate , second xargs picks up and save to .md5



                      If you need filter file patter, you can replace ls with find or add grep afterwards.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 1 at 19:10

























                      answered Feb 1 at 17:41









                      Ming Jia

                      12




                      12











                      • Parses output of ls and uses unquoted variable expansions.
                        – Kusalananda
                        Feb 1 at 17:43










                      • Can you explain your comments? you can replace ls with find or any other command.
                        – Ming Jia
                        Feb 1 at 19:12










                      • Assume spaces in any of the names in the files directory, for example. Also consider what would happen if one of the names is a directory.
                        – Kusalananda
                        Feb 2 at 6:33











                      • @MingJia Parsing output of ls is considered generally bad practice (see this ). Even if there aren't any difficult filenames, that doesn't mean one shouldn't anticipate them. Second big problem is the $0 and $1 should be double-quoted. Because of "word splitting", if you have file with spaces or tabs in filename, ` > $1` will have ambiguous redirect error. Try var="with space.txt"; echo "hi" > $var and then var="with space.txt"; echo "hi" > "$var"
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:55










                      • @MingJia Don't get discouraged, you'll learn to pick up on these things if you stick around on the site, with time of course. Welcome, to the site and consider re-working your answer.
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:57
















                      • Parses output of ls and uses unquoted variable expansions.
                        – Kusalananda
                        Feb 1 at 17:43










                      • Can you explain your comments? you can replace ls with find or any other command.
                        – Ming Jia
                        Feb 1 at 19:12










                      • Assume spaces in any of the names in the files directory, for example. Also consider what would happen if one of the names is a directory.
                        – Kusalananda
                        Feb 2 at 6:33











                      • @MingJia Parsing output of ls is considered generally bad practice (see this ). Even if there aren't any difficult filenames, that doesn't mean one shouldn't anticipate them. Second big problem is the $0 and $1 should be double-quoted. Because of "word splitting", if you have file with spaces or tabs in filename, ` > $1` will have ambiguous redirect error. Try var="with space.txt"; echo "hi" > $var and then var="with space.txt"; echo "hi" > "$var"
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:55










                      • @MingJia Don't get discouraged, you'll learn to pick up on these things if you stick around on the site, with time of course. Welcome, to the site and consider re-working your answer.
                        – Sergiy Kolodyazhnyy
                        Feb 2 at 6:57















                      Parses output of ls and uses unquoted variable expansions.
                      – Kusalananda
                      Feb 1 at 17:43




                      Parses output of ls and uses unquoted variable expansions.
                      – Kusalananda
                      Feb 1 at 17:43












                      Can you explain your comments? you can replace ls with find or any other command.
                      – Ming Jia
                      Feb 1 at 19:12




                      Can you explain your comments? you can replace ls with find or any other command.
                      – Ming Jia
                      Feb 1 at 19:12












                      Assume spaces in any of the names in the files directory, for example. Also consider what would happen if one of the names is a directory.
                      – Kusalananda
                      Feb 2 at 6:33





                      Assume spaces in any of the names in the files directory, for example. Also consider what would happen if one of the names is a directory.
                      – Kusalananda
                      Feb 2 at 6:33













                      @MingJia Parsing output of ls is considered generally bad practice (see this ). Even if there aren't any difficult filenames, that doesn't mean one shouldn't anticipate them. Second big problem is the $0 and $1 should be double-quoted. Because of "word splitting", if you have file with spaces or tabs in filename, ` > $1` will have ambiguous redirect error. Try var="with space.txt"; echo "hi" > $var and then var="with space.txt"; echo "hi" > "$var"
                      – Sergiy Kolodyazhnyy
                      Feb 2 at 6:55




                      @MingJia Parsing output of ls is considered generally bad practice (see this ). Even if there aren't any difficult filenames, that doesn't mean one shouldn't anticipate them. Second big problem is the $0 and $1 should be double-quoted. Because of "word splitting", if you have file with spaces or tabs in filename, ` > $1` will have ambiguous redirect error. Try var="with space.txt"; echo "hi" > $var and then var="with space.txt"; echo "hi" > "$var"
                      – Sergiy Kolodyazhnyy
                      Feb 2 at 6:55












                      @MingJia Don't get discouraged, you'll learn to pick up on these things if you stick around on the site, with time of course. Welcome, to the site and consider re-working your answer.
                      – Sergiy Kolodyazhnyy
                      Feb 2 at 6:57




                      @MingJia Don't get discouraged, you'll learn to pick up on these things if you stick around on the site, with time of course. Welcome, to the site and consider re-working your answer.
                      – Sergiy Kolodyazhnyy
                      Feb 2 at 6:57

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402907%2fgenerate-md5sum-for-all-files-in-a-directory-and-then-write-filename-md5-for%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)