Checking File Sums between 2 directories

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











up vote
1
down vote

favorite












I would like to check the file check sums between 2 directories.



I have written the following script to do so:



#!/bin/bash

echo Checking File Copy

SRC_DIR=/home/user/src

echo Source Directory: $SRC_DIR

TGT_DIR=/media/user/tgt

echo Target Directory: $TGT_DIR

cd $SRC_DIR

for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done


The script works fine until it encounters a file with space in the name at which point I get an error.



I tried to solve it by "escaping" the file name like so:



tgt_file=$file_name// /\ 


Unfortunately that causes an error when reading the file names.



I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.







share|improve this question
















  • 2




    Why don't you just double quote the variable expansions instead?
    – Kusalananda
    Nov 11 '17 at 16:22














up vote
1
down vote

favorite












I would like to check the file check sums between 2 directories.



I have written the following script to do so:



#!/bin/bash

echo Checking File Copy

SRC_DIR=/home/user/src

echo Source Directory: $SRC_DIR

TGT_DIR=/media/user/tgt

echo Target Directory: $TGT_DIR

cd $SRC_DIR

for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done


The script works fine until it encounters a file with space in the name at which point I get an error.



I tried to solve it by "escaping" the file name like so:



tgt_file=$file_name// /\ 


Unfortunately that causes an error when reading the file names.



I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.







share|improve this question
















  • 2




    Why don't you just double quote the variable expansions instead?
    – Kusalananda
    Nov 11 '17 at 16:22












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like to check the file check sums between 2 directories.



I have written the following script to do so:



#!/bin/bash

echo Checking File Copy

SRC_DIR=/home/user/src

echo Source Directory: $SRC_DIR

TGT_DIR=/media/user/tgt

echo Target Directory: $TGT_DIR

cd $SRC_DIR

for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done


The script works fine until it encounters a file with space in the name at which point I get an error.



I tried to solve it by "escaping" the file name like so:



tgt_file=$file_name// /\ 


Unfortunately that causes an error when reading the file names.



I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.







share|improve this question












I would like to check the file check sums between 2 directories.



I have written the following script to do so:



#!/bin/bash

echo Checking File Copy

SRC_DIR=/home/user/src

echo Source Directory: $SRC_DIR

TGT_DIR=/media/user/tgt

echo Target Directory: $TGT_DIR

cd $SRC_DIR

for file_name in *.txt
do
echo $file_name
tgt_file=$file_name// /\
sum $TGT_DIR/$tgt_file
sum $file_name
done


The script works fine until it encounters a file with space in the name at which point I get an error.



I tried to solve it by "escaping" the file name like so:



tgt_file=$file_name// /\ 


Unfortunately that causes an error when reading the file names.



I have searched but have not found an answer that seems to work. Any suggestions would be very welcome. Thanks.









share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '17 at 16:20









earnshae

61




61







  • 2




    Why don't you just double quote the variable expansions instead?
    – Kusalananda
    Nov 11 '17 at 16:22












  • 2




    Why don't you just double quote the variable expansions instead?
    – Kusalananda
    Nov 11 '17 at 16:22







2




2




Why don't you just double quote the variable expansions instead?
– Kusalananda
Nov 11 '17 at 16:22




Why don't you just double quote the variable expansions instead?
– Kusalananda
Nov 11 '17 at 16:22










2 Answers
2






active

oldest

votes

















up vote
3
down vote













You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.



For further discussion on why this is important see the following post:



  • Why does my shell script choke on whitespace or other special characters?

Here is what your script should probably look like:



#!/bin/bash

echo "Checking File Copy"

SRC_DIR="/home/user/src"

echo "Source Directory: $SRC_DIR"

TGT_DIR="/media/user/tgt"

echo "Target Directory: $TGT_DIR"

cd "$SRC_DIR"

for file_name in *.txt
do
echo "$file_name"
sum "$TGT_DIR/$file_name"
sum "$file_name"
done


You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.



I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:



  • When do we need curly braces around shell variables?





share|improve this answer





























    up vote
    0
    down vote













    diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)


    ls -1 gives you a list of files, 1 each line



    wc -l count the lines



    EDIT:



    for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); 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',
      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%2f403923%2fchecking-file-sums-between-2-directories%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.



      For further discussion on why this is important see the following post:



      • Why does my shell script choke on whitespace or other special characters?

      Here is what your script should probably look like:



      #!/bin/bash

      echo "Checking File Copy"

      SRC_DIR="/home/user/src"

      echo "Source Directory: $SRC_DIR"

      TGT_DIR="/media/user/tgt"

      echo "Target Directory: $TGT_DIR"

      cd "$SRC_DIR"

      for file_name in *.txt
      do
      echo "$file_name"
      sum "$TGT_DIR/$file_name"
      sum "$file_name"
      done


      You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.



      I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:



      • When do we need curly braces around shell variables?





      share|improve this answer


























        up vote
        3
        down vote













        You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.



        For further discussion on why this is important see the following post:



        • Why does my shell script choke on whitespace or other special characters?

        Here is what your script should probably look like:



        #!/bin/bash

        echo "Checking File Copy"

        SRC_DIR="/home/user/src"

        echo "Source Directory: $SRC_DIR"

        TGT_DIR="/media/user/tgt"

        echo "Target Directory: $TGT_DIR"

        cd "$SRC_DIR"

        for file_name in *.txt
        do
        echo "$file_name"
        sum "$TGT_DIR/$file_name"
        sum "$file_name"
        done


        You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.



        I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:



        • When do we need curly braces around shell variables?





        share|improve this answer
























          up vote
          3
          down vote










          up vote
          3
          down vote









          You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.



          For further discussion on why this is important see the following post:



          • Why does my shell script choke on whitespace or other special characters?

          Here is what your script should probably look like:



          #!/bin/bash

          echo "Checking File Copy"

          SRC_DIR="/home/user/src"

          echo "Source Directory: $SRC_DIR"

          TGT_DIR="/media/user/tgt"

          echo "Target Directory: $TGT_DIR"

          cd "$SRC_DIR"

          for file_name in *.txt
          do
          echo "$file_name"
          sum "$TGT_DIR/$file_name"
          sum "$file_name"
          done


          You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.



          I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:



          • When do we need curly braces around shell variables?





          share|improve this answer














          You're not double-quoting any of your variables. Double-quoting your strings (including strings with variables) will cause them to be treated as a single token. Otherwise they will be treated as white-space delimited lists of tokens. This applies even if the white-space occurs after parameter-expansion - as is happening in your situation.



          For further discussion on why this is important see the following post:



          • Why does my shell script choke on whitespace or other special characters?

          Here is what your script should probably look like:



          #!/bin/bash

          echo "Checking File Copy"

          SRC_DIR="/home/user/src"

          echo "Source Directory: $SRC_DIR"

          TGT_DIR="/media/user/tgt"

          echo "Target Directory: $TGT_DIR"

          cd "$SRC_DIR"

          for file_name in *.txt
          do
          echo "$file_name"
          sum "$TGT_DIR/$file_name"
          sum "$file_name"
          done


          You'll notice that I've quoted the strings as well as the variables. Strictly speaking this isn't really necessary here, but it's probably good to get in the habit of quoting everything that you want to consider as a single token.



          I also used the curly-brace syntax for your variables. Again, this probably isn't necessary in this exact situation, but I would consider it a best-practice in general. For further discussion regarding the brace syntax see the following post:



          • When do we need curly braces around shell variables?






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 '17 at 16:32

























          answered Nov 11 '17 at 16:26









          igal

          4,830930




          4,830930






















              up vote
              0
              down vote













              diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)


              ls -1 gives you a list of files, 1 each line



              wc -l count the lines



              EDIT:



              for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done





              share|improve this answer


























                up vote
                0
                down vote













                diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)


                ls -1 gives you a list of files, 1 each line



                wc -l count the lines



                EDIT:



                for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done





                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)


                  ls -1 gives you a list of files, 1 each line



                  wc -l count the lines



                  EDIT:



                  for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done





                  share|improve this answer














                  diff -q <(ls -1 $dir1 | wc -l) <(ls -1 $dir2 | wc -l)


                  ls -1 gives you a list of files, 1 each line



                  wc -l count the lines



                  EDIT:



                  for file in $(sort -u <(ls -1 $dir1/; ls -1 $dir2/)); do echo $file; diff -q <(checksumcommand $dir1/$file) <(checksumcommand $dir2/$file); done






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 '17 at 21:05

























                  answered Nov 11 '17 at 20:46









                  FaxMax

                  428219




                  428219



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403923%2fchecking-file-sums-between-2-directories%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?