Redirect stdout / stderr output to log file with timestamp

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











up vote
0
down vote

favorite












I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.



I can create the filename with the following command:



$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt


So I want to do something like this:



mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)


But this doesn't work (-bash: syntax error near unexpected token('`)







share|improve this question




















  • Similar (no dupe): unix.stackexchange.com/questions/436800/…
    – Kusalananda
    Apr 10 at 17:03














up vote
0
down vote

favorite












I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.



I can create the filename with the following command:



$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt


So I want to do something like this:



mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)


But this doesn't work (-bash: syntax error near unexpected token('`)







share|improve this question




















  • Similar (no dupe): unix.stackexchange.com/questions/436800/…
    – Kusalananda
    Apr 10 at 17:03












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.



I can create the filename with the following command:



$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt


So I want to do something like this:



mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)


But this doesn't work (-bash: syntax error near unexpected token('`)







share|improve this question












I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.



I can create the filename with the following command:



$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt


So I want to do something like this:



mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)


But this doesn't work (-bash: syntax error near unexpected token('`)









share|improve this question











share|improve this question




share|improve this question










asked Apr 10 at 14:01









Caner

1033




1033











  • Similar (no dupe): unix.stackexchange.com/questions/436800/…
    – Kusalananda
    Apr 10 at 17:03
















  • Similar (no dupe): unix.stackexchange.com/questions/436800/…
    – Kusalananda
    Apr 10 at 17:03















Similar (no dupe): unix.stackexchange.com/questions/436800/…
– Kusalananda
Apr 10 at 17:03




Similar (no dupe): unix.stackexchange.com/questions/436800/…
– Kusalananda
Apr 10 at 17:03










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Yes, you will need to use a command substitution:



mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"


Which is bash-speak for



mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1


Which is the same as



mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1


(%F is the same as %Y-%m-%d)



A command substitution, $(...), will be replaced by the output of the command inside.



What you used was a sub-shell, (...). A sub-shell can't accept redirections like that.






share|improve this answer





























    up vote
    1
    down vote













    Try this:



    today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1





    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%2f436784%2fredirect-stdout-stderr-output-to-log-file-with-timestamp%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
      2
      down vote



      accepted










      Yes, you will need to use a command substitution:



      mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"


      Which is bash-speak for



      mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1


      Which is the same as



      mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1


      (%F is the same as %Y-%m-%d)



      A command substitution, $(...), will be replaced by the output of the command inside.



      What you used was a sub-shell, (...). A sub-shell can't accept redirections like that.






      share|improve this answer


























        up vote
        2
        down vote



        accepted










        Yes, you will need to use a command substitution:



        mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"


        Which is bash-speak for



        mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1


        Which is the same as



        mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1


        (%F is the same as %Y-%m-%d)



        A command substitution, $(...), will be replaced by the output of the command inside.



        What you used was a sub-shell, (...). A sub-shell can't accept redirections like that.






        share|improve this answer
























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Yes, you will need to use a command substitution:



          mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"


          Which is bash-speak for



          mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1


          Which is the same as



          mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1


          (%F is the same as %Y-%m-%d)



          A command substitution, $(...), will be replaced by the output of the command inside.



          What you used was a sub-shell, (...). A sub-shell can't accept redirections like that.






          share|improve this answer














          Yes, you will need to use a command substitution:



          mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"


          Which is bash-speak for



          mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1


          Which is the same as



          mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1


          (%F is the same as %Y-%m-%d)



          A command substitution, $(...), will be replaced by the output of the command inside.



          What you used was a sub-shell, (...). A sub-shell can't accept redirections like that.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 10 at 14:36

























          answered Apr 10 at 14:20









          Kusalananda

          102k13200317




          102k13200317






















              up vote
              1
              down vote













              Try this:



              today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1





              share|improve this answer
























                up vote
                1
                down vote













                Try this:



                today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Try this:



                  today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1





                  share|improve this answer












                  Try this:



                  today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 10 at 14:20









                  L.Ray

                  1967




                  1967






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436784%2fredirect-stdout-stderr-output-to-log-file-with-timestamp%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?