Compare file dates

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











up vote
1
down vote

favorite












Sometimes when I boot, my system goes into emergency mode.



I then use Clonezilla to restore an image.



Usually the image is older than the current date.



This is part of a backup script that runs as a startup program.



cd /home/andy/bin/
zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/


This then overwrites my zip file with one that is in fact older.



I thought that if I could compare the file dates, before the zip operation, I could prevent the overwriting.



I tried this.



file1time=`stat -c %Y /home/andy/bin/Ubuntu_Scripts.zip`

file2time=`stat -c %Y /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Scripts.zip`

if [ "$file1time" -ot "$file2time" ]
then
echo "File is older. "
fi






share|improve this question
























    up vote
    1
    down vote

    favorite












    Sometimes when I boot, my system goes into emergency mode.



    I then use Clonezilla to restore an image.



    Usually the image is older than the current date.



    This is part of a backup script that runs as a startup program.



    cd /home/andy/bin/
    zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
    cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
    cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/


    This then overwrites my zip file with one that is in fact older.



    I thought that if I could compare the file dates, before the zip operation, I could prevent the overwriting.



    I tried this.



    file1time=`stat -c %Y /home/andy/bin/Ubuntu_Scripts.zip`

    file2time=`stat -c %Y /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Scripts.zip`

    if [ "$file1time" -ot "$file2time" ]
    then
    echo "File is older. "
    fi






    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Sometimes when I boot, my system goes into emergency mode.



      I then use Clonezilla to restore an image.



      Usually the image is older than the current date.



      This is part of a backup script that runs as a startup program.



      cd /home/andy/bin/
      zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
      cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
      cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/


      This then overwrites my zip file with one that is in fact older.



      I thought that if I could compare the file dates, before the zip operation, I could prevent the overwriting.



      I tried this.



      file1time=`stat -c %Y /home/andy/bin/Ubuntu_Scripts.zip`

      file2time=`stat -c %Y /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Scripts.zip`

      if [ "$file1time" -ot "$file2time" ]
      then
      echo "File is older. "
      fi






      share|improve this question












      Sometimes when I boot, my system goes into emergency mode.



      I then use Clonezilla to restore an image.



      Usually the image is older than the current date.



      This is part of a backup script that runs as a startup program.



      cd /home/andy/bin/
      zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
      cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
      cp -u Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/


      This then overwrites my zip file with one that is in fact older.



      I thought that if I could compare the file dates, before the zip operation, I could prevent the overwriting.



      I tried this.



      file1time=`stat -c %Y /home/andy/bin/Ubuntu_Scripts.zip`

      file2time=`stat -c %Y /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Scripts.zip`

      if [ "$file1time" -ot "$file2time" ]
      then
      echo "File is older. "
      fi








      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 9 at 13:04









      fixit7

      491317




      491317




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Use rsync instead of cp and you can use the --update flag that will only update when you are copying a newer file.



          cd /home/andy/bin/
          zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
          rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
          rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/





          share|improve this answer



























            up vote
            2
            down vote













            Your code uses -ot where it should use -lt. See the example below.



            #!/bin/bash

            file1time=`stat -c %Y /home/niclas/dbat/INSTALL.txt`

            file2time=`stat -c %Y /home/niclas/dbat/README.txt`

            if [ "$file1time" -lt "$file2time" ]
            then
            echo "INSTALL.txt is older."
            else
            echo "README.txt is older."
            fi





            share|improve this answer




















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "89"
              ;
              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: true,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2faskubuntu.com%2fquestions%2f1063832%2fcompare-file-dates%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



              accepted










              Use rsync instead of cp and you can use the --update flag that will only update when you are copying a newer file.



              cd /home/andy/bin/
              zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
              rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
              rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/





              share|improve this answer
























                up vote
                3
                down vote



                accepted










                Use rsync instead of cp and you can use the --update flag that will only update when you are copying a newer file.



                cd /home/andy/bin/
                zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
                rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
                rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/





                share|improve this answer






















                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  Use rsync instead of cp and you can use the --update flag that will only update when you are copying a newer file.



                  cd /home/andy/bin/
                  zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
                  rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
                  rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/





                  share|improve this answer












                  Use rsync instead of cp and you can use the --update flag that will only update when you are copying a newer file.



                  cd /home/andy/bin/
                  zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py *.txt
                  rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/
                  rsync -av --update Ubuntu_Scripts.zip /media/andy/MAXTOR_SDB5/Emergency_Backup/






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 9 at 13:18









                  Katu

                  2,2051326




                  2,2051326






















                      up vote
                      2
                      down vote













                      Your code uses -ot where it should use -lt. See the example below.



                      #!/bin/bash

                      file1time=`stat -c %Y /home/niclas/dbat/INSTALL.txt`

                      file2time=`stat -c %Y /home/niclas/dbat/README.txt`

                      if [ "$file1time" -lt "$file2time" ]
                      then
                      echo "INSTALL.txt is older."
                      else
                      echo "README.txt is older."
                      fi





                      share|improve this answer
























                        up vote
                        2
                        down vote













                        Your code uses -ot where it should use -lt. See the example below.



                        #!/bin/bash

                        file1time=`stat -c %Y /home/niclas/dbat/INSTALL.txt`

                        file2time=`stat -c %Y /home/niclas/dbat/README.txt`

                        if [ "$file1time" -lt "$file2time" ]
                        then
                        echo "INSTALL.txt is older."
                        else
                        echo "README.txt is older."
                        fi





                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          Your code uses -ot where it should use -lt. See the example below.



                          #!/bin/bash

                          file1time=`stat -c %Y /home/niclas/dbat/INSTALL.txt`

                          file2time=`stat -c %Y /home/niclas/dbat/README.txt`

                          if [ "$file1time" -lt "$file2time" ]
                          then
                          echo "INSTALL.txt is older."
                          else
                          echo "README.txt is older."
                          fi





                          share|improve this answer












                          Your code uses -ot where it should use -lt. See the example below.



                          #!/bin/bash

                          file1time=`stat -c %Y /home/niclas/dbat/INSTALL.txt`

                          file2time=`stat -c %Y /home/niclas/dbat/README.txt`

                          if [ "$file1time" -lt "$file2time" ]
                          then
                          echo "INSTALL.txt is older."
                          else
                          echo "README.txt is older."
                          fi






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 9 at 13:26









                          Niclas Börlin

                          7961515




                          7961515



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1063832%2fcompare-file-dates%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              Installing NextGIS Connect into QGIS 3?

                              One-line joke