Backup text file using date string

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











up vote
1
down vote

favorite












I am trying to append a date string to the file name to back up a text file

So I did:



cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"


But its not working, how can I append the output of "date" in a string?



user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
20171222_075003
user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
date +%Y%m%d_%H%M%S






share|improve this question


























    up vote
    1
    down vote

    favorite












    I am trying to append a date string to the file name to back up a text file

    So I did:



    cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"


    But its not working, how can I append the output of "date" in a string?



    user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
    20171222_075003
    user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
    date +%Y%m%d_%H%M%S






    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to append a date string to the file name to back up a text file

      So I did:



      cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"


      But its not working, how can I append the output of "date" in a string?



      user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
      20171222_075003
      user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
      date +%Y%m%d_%H%M%S






      share|improve this question














      I am trying to append a date string to the file name to back up a text file

      So I did:



      cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"


      But its not working, how can I append the output of "date" in a string?



      user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
      20171222_075003
      user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
      date +%Y%m%d_%H%M%S








      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 22 '17 at 16:00

























      asked Dec 22 '17 at 15:55









      user648026

      1084




      1084




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Use the following:



          cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")



          • $infile - variable $infile interpolated within concatenated string


          • $(date +"%Y%m%d_%H%M%S") - command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)


          If the date format specifier would contain whitespace(s) like %Y%m%d %H%M%S - wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"






          share|improve this answer






















          • Thank you! This works! can you explain the magic?
            – user648026
            Dec 22 '17 at 16:06











          • @user648026, yes, see the explanation
            – RomanPerekhrest
            Dec 22 '17 at 16:09










          • +1. But...while this is safe enough with this date command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")". This works correctly even though it looks like the double-quotes are nested, because the $(date...) is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
            – cas
            Dec 23 '17 at 6:07











          • @cas, ok, added notation
            – RomanPerekhrest
            Dec 23 '17 at 8:49






          • 1




            You might want cp -p instead of plain cp to preserve the metadata such as timestamps and permissions.
            – roaima
            Dec 23 '17 at 15:11

















          up vote
          1
          down vote













          I have done by below method



          i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i


          where i is variable which contains date command output






          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%2f412540%2fbackup-text-file-using-date-string%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 the following:



            cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")



            • $infile - variable $infile interpolated within concatenated string


            • $(date +"%Y%m%d_%H%M%S") - command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)


            If the date format specifier would contain whitespace(s) like %Y%m%d %H%M%S - wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"






            share|improve this answer






















            • Thank you! This works! can you explain the magic?
              – user648026
              Dec 22 '17 at 16:06











            • @user648026, yes, see the explanation
              – RomanPerekhrest
              Dec 22 '17 at 16:09










            • +1. But...while this is safe enough with this date command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")". This works correctly even though it looks like the double-quotes are nested, because the $(date...) is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
              – cas
              Dec 23 '17 at 6:07











            • @cas, ok, added notation
              – RomanPerekhrest
              Dec 23 '17 at 8:49






            • 1




              You might want cp -p instead of plain cp to preserve the metadata such as timestamps and permissions.
              – roaima
              Dec 23 '17 at 15:11














            up vote
            3
            down vote



            accepted










            Use the following:



            cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")



            • $infile - variable $infile interpolated within concatenated string


            • $(date +"%Y%m%d_%H%M%S") - command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)


            If the date format specifier would contain whitespace(s) like %Y%m%d %H%M%S - wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"






            share|improve this answer






















            • Thank you! This works! can you explain the magic?
              – user648026
              Dec 22 '17 at 16:06











            • @user648026, yes, see the explanation
              – RomanPerekhrest
              Dec 22 '17 at 16:09










            • +1. But...while this is safe enough with this date command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")". This works correctly even though it looks like the double-quotes are nested, because the $(date...) is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
              – cas
              Dec 23 '17 at 6:07











            • @cas, ok, added notation
              – RomanPerekhrest
              Dec 23 '17 at 8:49






            • 1




              You might want cp -p instead of plain cp to preserve the metadata such as timestamps and permissions.
              – roaima
              Dec 23 '17 at 15:11












            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            Use the following:



            cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")



            • $infile - variable $infile interpolated within concatenated string


            • $(date +"%Y%m%d_%H%M%S") - command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)


            If the date format specifier would contain whitespace(s) like %Y%m%d %H%M%S - wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"






            share|improve this answer














            Use the following:



            cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")



            • $infile - variable $infile interpolated within concatenated string


            • $(date +"%Y%m%d_%H%M%S") - command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)


            If the date format specifier would contain whitespace(s) like %Y%m%d %H%M%S - wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 23 '17 at 8:49

























            answered Dec 22 '17 at 16:00









            RomanPerekhrest

            22.4k12145




            22.4k12145











            • Thank you! This works! can you explain the magic?
              – user648026
              Dec 22 '17 at 16:06











            • @user648026, yes, see the explanation
              – RomanPerekhrest
              Dec 22 '17 at 16:09










            • +1. But...while this is safe enough with this date command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")". This works correctly even though it looks like the double-quotes are nested, because the $(date...) is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
              – cas
              Dec 23 '17 at 6:07











            • @cas, ok, added notation
              – RomanPerekhrest
              Dec 23 '17 at 8:49






            • 1




              You might want cp -p instead of plain cp to preserve the metadata such as timestamps and permissions.
              – roaima
              Dec 23 '17 at 15:11
















            • Thank you! This works! can you explain the magic?
              – user648026
              Dec 22 '17 at 16:06











            • @user648026, yes, see the explanation
              – RomanPerekhrest
              Dec 22 '17 at 16:09










            • +1. But...while this is safe enough with this date command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")". This works correctly even though it looks like the double-quotes are nested, because the $(date...) is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
              – cas
              Dec 23 '17 at 6:07











            • @cas, ok, added notation
              – RomanPerekhrest
              Dec 23 '17 at 8:49






            • 1




              You might want cp -p instead of plain cp to preserve the metadata such as timestamps and permissions.
              – roaima
              Dec 23 '17 at 15:11















            Thank you! This works! can you explain the magic?
            – user648026
            Dec 22 '17 at 16:06





            Thank you! This works! can you explain the magic?
            – user648026
            Dec 22 '17 at 16:06













            @user648026, yes, see the explanation
            – RomanPerekhrest
            Dec 22 '17 at 16:09




            @user648026, yes, see the explanation
            – RomanPerekhrest
            Dec 22 '17 at 16:09












            +1. But...while this is safe enough with this date command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")". This works correctly even though it looks like the double-quotes are nested, because the $(date...) is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
            – cas
            Dec 23 '17 at 6:07





            +1. But...while this is safe enough with this date command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")". This works correctly even though it looks like the double-quotes are nested, because the $(date...) is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
            – cas
            Dec 23 '17 at 6:07













            @cas, ok, added notation
            – RomanPerekhrest
            Dec 23 '17 at 8:49




            @cas, ok, added notation
            – RomanPerekhrest
            Dec 23 '17 at 8:49




            1




            1




            You might want cp -p instead of plain cp to preserve the metadata such as timestamps and permissions.
            – roaima
            Dec 23 '17 at 15:11




            You might want cp -p instead of plain cp to preserve the metadata such as timestamps and permissions.
            – roaima
            Dec 23 '17 at 15:11












            up vote
            1
            down vote













            I have done by below method



            i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i


            where i is variable which contains date command output






            share|improve this answer
























              up vote
              1
              down vote













              I have done by below method



              i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i


              where i is variable which contains date command output






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                I have done by below method



                i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i


                where i is variable which contains date command output






                share|improve this answer












                I have done by below method



                i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i


                where i is variable which contains date command output







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 23 '17 at 8:42









                Praveen Kumar BS

                1,010128




                1,010128






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412540%2fbackup-text-file-using-date-string%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?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay