Get file modification time in a specific format (yyyymmddhh24miss)

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











up vote
3
down vote

favorite












I want to get the modification time of a file in a specific format.

How can I do that ?

I know about



stat -c %x find.txt


but I need this format:



yyyymmddhh24miss


I'm using ksh on Linux 2.6.18-406.el5 x86_64 if it matters.










share|improve this question























  • unix and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:53










  • Linux 2.6.18-406.el5 x86_64 and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:55














up vote
3
down vote

favorite












I want to get the modification time of a file in a specific format.

How can I do that ?

I know about



stat -c %x find.txt


but I need this format:



yyyymmddhh24miss


I'm using ksh on Linux 2.6.18-406.el5 x86_64 if it matters.










share|improve this question























  • unix and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:53










  • Linux 2.6.18-406.el5 x86_64 and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:55












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I want to get the modification time of a file in a specific format.

How can I do that ?

I know about



stat -c %x find.txt


but I need this format:



yyyymmddhh24miss


I'm using ksh on Linux 2.6.18-406.el5 x86_64 if it matters.










share|improve this question















I want to get the modification time of a file in a specific format.

How can I do that ?

I know about



stat -c %x find.txt


but I need this format:



yyyymmddhh24miss


I'm using ksh on Linux 2.6.18-406.el5 x86_64 if it matters.







linux date stat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 mins ago









don_crissti

48.3k15129158




48.3k15129158










asked May 27 '16 at 12:40









ANOUK_prog

72210




72210











  • unix and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:53










  • Linux 2.6.18-406.el5 x86_64 and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:55
















  • unix and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:53










  • Linux 2.6.18-406.el5 x86_64 and /bin/ksh
    – ANOUK_prog
    May 27 '16 at 12:55















unix and /bin/ksh
– ANOUK_prog
May 27 '16 at 12:53




unix and /bin/ksh
– ANOUK_prog
May 27 '16 at 12:53












Linux 2.6.18-406.el5 x86_64 and /bin/ksh
– ANOUK_prog
May 27 '16 at 12:55




Linux 2.6.18-406.el5 x86_64 and /bin/ksh
– ANOUK_prog
May 27 '16 at 12:55










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










You can use something like:



/bin/date +%Y%m%d%H%M%S -d "$(/usr/bin/stat -c %x find.txt)"





share|improve this answer



























    up vote
    3
    down vote













    With GNU tools:



    find file -prune -printf '%TY%Tm%Td%TH%TM%TSn'


    Or



    date -r file +%Y%m%d%H%M%S.%N


    If your ksh is ksh93 and it has been built with the date builtin enabled:



    command /opt/ast/bin/date -m -f %Y%m%d%H%M%S.%N file


    (command /opt/ast/bin/date invokes the date builtin bound to /opt/ast/bin/date, if you add /opt/ast/bin at the front of $PATH, those builtins will be invoked automatically when you call them by name).



    If you don't care for the fractional part, remove the .%N or for find, replace %TS with %.2TS.



    Note that the time will be given in the current timezone. As this date format doesn't include the UTC offset, it can be ambiguous. For instance in my mainland British timezone:



    $ date -r file1
    Sun 30 Oct 01:00:00 BST 2016
    $ date -r file2
    Sun 30 Oct 01:00:00 GMT 2016


    Those two files have a modification time that are 3600 seconds apart, one before, one after the change to winter time but still at the same wall clock time (as that clock has been moved back one hour in the interval).



    $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
    20161030010000.0000000000
    20161030010000.0000000000


    To have unambiguous times, you can include the UTC offset:



    $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
    20161030010000.0000000000+0100
    20161030010000.0000000000+0000


    Or give the time in UTC:



    $ TZ=UTC0 find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
    20161030000000.0000000000
    20161030010000.0000000000





    share|improve this answer






















    • Unless file is a directory, -prune has no effect.
      – user79743
      May 29 '16 at 22:28

















    up vote
    1
    down vote













    The file modification time, could be formatted with date:



    $ date -d @"$(stat -c %Y file1)" +'%Y%m%d%H%M%S'


    Use %X for last access time and %Z for last change.



    However, the maximum resolution of %X, %Y and %Z is seconds.

    For nanoseconds resolution use the %x, %y and %z options:



    $ date -d "$(stat -c %y file1)" +'%Y%m%d%H%M%S.%N'
    20151101020000.012345678


    As we are using date already, we can simplify by using date's -r option, or we can use find's formatted output:



    $ date -r file1 +'%Y%m%d%H%M%S%z'
    20151101010000-0500
    $ find file1 -printf '%TY%Tm%Td%TH%TM%2.2TS%Tzn'
    20151101010000-0500


    The resolution of the above commands is seconds. If nanoseconds are needed:



    $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
    20151101010000.012345678-0500
    $ find file1 -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
    20151101010000.0123456780-0500


    Those two commands give modification time

    In date there is no way to get access or status time with the -r option.

    In find: Change %T to %A for access time and to %C for status change time




    About Time Zone



    Please note that all the above commands are affected by the time zone used:



    $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
    20151101010000.012345678
    $ TZ=GMT0 date -r file1 +'%Y%m%d%H%M%S.%N%z'
    20151101060000.012345678


    Note the Hour change from 01 to 06. That happens because the computer used had a time zone set for America/New_York. When the time is calculated at the GMT0 offset (aka GMT or GMT+0 or GMT-0) its value increase in 5 hours.



    Also, the use of local time values (if incomplete) may present equal values when they are actually different:



    $ date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N'
    20151101010000.012345678
    20151101010000.012345678


    That's why the tailing %z is needed for local times:



    $ date -r file1 +'%Y%m%d%H%M%S.%N%z'; date -r file2 +'%Y%m%d%H%M%S.%N%z'
    20151101010000.012345678-0500
    20151101010000.012345678-0400


    That makes the values different. But this kind of format makes sorting by time difficult. That is why the best solution is to use GMT times because the time offset (%z) is always 0 and can be omitted:



    $ ( TZ=GMT0; date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N')
    20151101060000.012345678
    20151101050000.012345678


    And sorting is possible.



    [1] Values used to touch files used.

    touch -m -d '20151101 01:00:00.012345678-05' file1

    touch -a -d '20151101 01:00:00.987654321-05' file1

    touch -m -d '20151101 01:00:00.012345678-04' file2

    touch -a -d '20151101 01:00:00.987654321-04' file2






    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: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      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%2f285897%2fget-file-modification-time-in-a-specific-format-yyyymmddhh24miss%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      You can use something like:



      /bin/date +%Y%m%d%H%M%S -d "$(/usr/bin/stat -c %x find.txt)"





      share|improve this answer
























        up vote
        1
        down vote



        accepted










        You can use something like:



        /bin/date +%Y%m%d%H%M%S -d "$(/usr/bin/stat -c %x find.txt)"





        share|improve this answer






















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can use something like:



          /bin/date +%Y%m%d%H%M%S -d "$(/usr/bin/stat -c %x find.txt)"





          share|improve this answer












          You can use something like:



          /bin/date +%Y%m%d%H%M%S -d "$(/usr/bin/stat -c %x find.txt)"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 27 '16 at 13:10









          Laurentiu Roescu

          50936




          50936






















              up vote
              3
              down vote













              With GNU tools:



              find file -prune -printf '%TY%Tm%Td%TH%TM%TSn'


              Or



              date -r file +%Y%m%d%H%M%S.%N


              If your ksh is ksh93 and it has been built with the date builtin enabled:



              command /opt/ast/bin/date -m -f %Y%m%d%H%M%S.%N file


              (command /opt/ast/bin/date invokes the date builtin bound to /opt/ast/bin/date, if you add /opt/ast/bin at the front of $PATH, those builtins will be invoked automatically when you call them by name).



              If you don't care for the fractional part, remove the .%N or for find, replace %TS with %.2TS.



              Note that the time will be given in the current timezone. As this date format doesn't include the UTC offset, it can be ambiguous. For instance in my mainland British timezone:



              $ date -r file1
              Sun 30 Oct 01:00:00 BST 2016
              $ date -r file2
              Sun 30 Oct 01:00:00 GMT 2016


              Those two files have a modification time that are 3600 seconds apart, one before, one after the change to winter time but still at the same wall clock time (as that clock has been moved back one hour in the interval).



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030010000.0000000000
              20161030010000.0000000000


              To have unambiguous times, you can include the UTC offset:



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
              20161030010000.0000000000+0100
              20161030010000.0000000000+0000


              Or give the time in UTC:



              $ TZ=UTC0 find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030000000.0000000000
              20161030010000.0000000000





              share|improve this answer






















              • Unless file is a directory, -prune has no effect.
                – user79743
                May 29 '16 at 22:28














              up vote
              3
              down vote













              With GNU tools:



              find file -prune -printf '%TY%Tm%Td%TH%TM%TSn'


              Or



              date -r file +%Y%m%d%H%M%S.%N


              If your ksh is ksh93 and it has been built with the date builtin enabled:



              command /opt/ast/bin/date -m -f %Y%m%d%H%M%S.%N file


              (command /opt/ast/bin/date invokes the date builtin bound to /opt/ast/bin/date, if you add /opt/ast/bin at the front of $PATH, those builtins will be invoked automatically when you call them by name).



              If you don't care for the fractional part, remove the .%N or for find, replace %TS with %.2TS.



              Note that the time will be given in the current timezone. As this date format doesn't include the UTC offset, it can be ambiguous. For instance in my mainland British timezone:



              $ date -r file1
              Sun 30 Oct 01:00:00 BST 2016
              $ date -r file2
              Sun 30 Oct 01:00:00 GMT 2016


              Those two files have a modification time that are 3600 seconds apart, one before, one after the change to winter time but still at the same wall clock time (as that clock has been moved back one hour in the interval).



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030010000.0000000000
              20161030010000.0000000000


              To have unambiguous times, you can include the UTC offset:



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
              20161030010000.0000000000+0100
              20161030010000.0000000000+0000


              Or give the time in UTC:



              $ TZ=UTC0 find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030000000.0000000000
              20161030010000.0000000000





              share|improve this answer






















              • Unless file is a directory, -prune has no effect.
                – user79743
                May 29 '16 at 22:28












              up vote
              3
              down vote










              up vote
              3
              down vote









              With GNU tools:



              find file -prune -printf '%TY%Tm%Td%TH%TM%TSn'


              Or



              date -r file +%Y%m%d%H%M%S.%N


              If your ksh is ksh93 and it has been built with the date builtin enabled:



              command /opt/ast/bin/date -m -f %Y%m%d%H%M%S.%N file


              (command /opt/ast/bin/date invokes the date builtin bound to /opt/ast/bin/date, if you add /opt/ast/bin at the front of $PATH, those builtins will be invoked automatically when you call them by name).



              If you don't care for the fractional part, remove the .%N or for find, replace %TS with %.2TS.



              Note that the time will be given in the current timezone. As this date format doesn't include the UTC offset, it can be ambiguous. For instance in my mainland British timezone:



              $ date -r file1
              Sun 30 Oct 01:00:00 BST 2016
              $ date -r file2
              Sun 30 Oct 01:00:00 GMT 2016


              Those two files have a modification time that are 3600 seconds apart, one before, one after the change to winter time but still at the same wall clock time (as that clock has been moved back one hour in the interval).



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030010000.0000000000
              20161030010000.0000000000


              To have unambiguous times, you can include the UTC offset:



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
              20161030010000.0000000000+0100
              20161030010000.0000000000+0000


              Or give the time in UTC:



              $ TZ=UTC0 find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030000000.0000000000
              20161030010000.0000000000





              share|improve this answer














              With GNU tools:



              find file -prune -printf '%TY%Tm%Td%TH%TM%TSn'


              Or



              date -r file +%Y%m%d%H%M%S.%N


              If your ksh is ksh93 and it has been built with the date builtin enabled:



              command /opt/ast/bin/date -m -f %Y%m%d%H%M%S.%N file


              (command /opt/ast/bin/date invokes the date builtin bound to /opt/ast/bin/date, if you add /opt/ast/bin at the front of $PATH, those builtins will be invoked automatically when you call them by name).



              If you don't care for the fractional part, remove the .%N or for find, replace %TS with %.2TS.



              Note that the time will be given in the current timezone. As this date format doesn't include the UTC offset, it can be ambiguous. For instance in my mainland British timezone:



              $ date -r file1
              Sun 30 Oct 01:00:00 BST 2016
              $ date -r file2
              Sun 30 Oct 01:00:00 GMT 2016


              Those two files have a modification time that are 3600 seconds apart, one before, one after the change to winter time but still at the same wall clock time (as that clock has been moved back one hour in the interval).



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030010000.0000000000
              20161030010000.0000000000


              To have unambiguous times, you can include the UTC offset:



              $ find file? -prune -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
              20161030010000.0000000000+0100
              20161030010000.0000000000+0000


              Or give the time in UTC:



              $ TZ=UTC0 find file? -prune -printf '%TY%Tm%Td%TH%TM%TSn'
              20161030000000.0000000000
              20161030010000.0000000000






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 27 '16 at 13:45

























              answered May 27 '16 at 13:22









              Stéphane Chazelas

              291k54543882




              291k54543882











              • Unless file is a directory, -prune has no effect.
                – user79743
                May 29 '16 at 22:28
















              • Unless file is a directory, -prune has no effect.
                – user79743
                May 29 '16 at 22:28















              Unless file is a directory, -prune has no effect.
              – user79743
              May 29 '16 at 22:28




              Unless file is a directory, -prune has no effect.
              – user79743
              May 29 '16 at 22:28










              up vote
              1
              down vote













              The file modification time, could be formatted with date:



              $ date -d @"$(stat -c %Y file1)" +'%Y%m%d%H%M%S'


              Use %X for last access time and %Z for last change.



              However, the maximum resolution of %X, %Y and %Z is seconds.

              For nanoseconds resolution use the %x, %y and %z options:



              $ date -d "$(stat -c %y file1)" +'%Y%m%d%H%M%S.%N'
              20151101020000.012345678


              As we are using date already, we can simplify by using date's -r option, or we can use find's formatted output:



              $ date -r file1 +'%Y%m%d%H%M%S%z'
              20151101010000-0500
              $ find file1 -printf '%TY%Tm%Td%TH%TM%2.2TS%Tzn'
              20151101010000-0500


              The resolution of the above commands is seconds. If nanoseconds are needed:



              $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
              20151101010000.012345678-0500
              $ find file1 -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
              20151101010000.0123456780-0500


              Those two commands give modification time

              In date there is no way to get access or status time with the -r option.

              In find: Change %T to %A for access time and to %C for status change time




              About Time Zone



              Please note that all the above commands are affected by the time zone used:



              $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
              20151101010000.012345678
              $ TZ=GMT0 date -r file1 +'%Y%m%d%H%M%S.%N%z'
              20151101060000.012345678


              Note the Hour change from 01 to 06. That happens because the computer used had a time zone set for America/New_York. When the time is calculated at the GMT0 offset (aka GMT or GMT+0 or GMT-0) its value increase in 5 hours.



              Also, the use of local time values (if incomplete) may present equal values when they are actually different:



              $ date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N'
              20151101010000.012345678
              20151101010000.012345678


              That's why the tailing %z is needed for local times:



              $ date -r file1 +'%Y%m%d%H%M%S.%N%z'; date -r file2 +'%Y%m%d%H%M%S.%N%z'
              20151101010000.012345678-0500
              20151101010000.012345678-0400


              That makes the values different. But this kind of format makes sorting by time difficult. That is why the best solution is to use GMT times because the time offset (%z) is always 0 and can be omitted:



              $ ( TZ=GMT0; date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N')
              20151101060000.012345678
              20151101050000.012345678


              And sorting is possible.



              [1] Values used to touch files used.

              touch -m -d '20151101 01:00:00.012345678-05' file1

              touch -a -d '20151101 01:00:00.987654321-05' file1

              touch -m -d '20151101 01:00:00.012345678-04' file2

              touch -a -d '20151101 01:00:00.987654321-04' file2






              share|improve this answer


























                up vote
                1
                down vote













                The file modification time, could be formatted with date:



                $ date -d @"$(stat -c %Y file1)" +'%Y%m%d%H%M%S'


                Use %X for last access time and %Z for last change.



                However, the maximum resolution of %X, %Y and %Z is seconds.

                For nanoseconds resolution use the %x, %y and %z options:



                $ date -d "$(stat -c %y file1)" +'%Y%m%d%H%M%S.%N'
                20151101020000.012345678


                As we are using date already, we can simplify by using date's -r option, or we can use find's formatted output:



                $ date -r file1 +'%Y%m%d%H%M%S%z'
                20151101010000-0500
                $ find file1 -printf '%TY%Tm%Td%TH%TM%2.2TS%Tzn'
                20151101010000-0500


                The resolution of the above commands is seconds. If nanoseconds are needed:



                $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
                20151101010000.012345678-0500
                $ find file1 -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
                20151101010000.0123456780-0500


                Those two commands give modification time

                In date there is no way to get access or status time with the -r option.

                In find: Change %T to %A for access time and to %C for status change time




                About Time Zone



                Please note that all the above commands are affected by the time zone used:



                $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
                20151101010000.012345678
                $ TZ=GMT0 date -r file1 +'%Y%m%d%H%M%S.%N%z'
                20151101060000.012345678


                Note the Hour change from 01 to 06. That happens because the computer used had a time zone set for America/New_York. When the time is calculated at the GMT0 offset (aka GMT or GMT+0 or GMT-0) its value increase in 5 hours.



                Also, the use of local time values (if incomplete) may present equal values when they are actually different:



                $ date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N'
                20151101010000.012345678
                20151101010000.012345678


                That's why the tailing %z is needed for local times:



                $ date -r file1 +'%Y%m%d%H%M%S.%N%z'; date -r file2 +'%Y%m%d%H%M%S.%N%z'
                20151101010000.012345678-0500
                20151101010000.012345678-0400


                That makes the values different. But this kind of format makes sorting by time difficult. That is why the best solution is to use GMT times because the time offset (%z) is always 0 and can be omitted:



                $ ( TZ=GMT0; date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N')
                20151101060000.012345678
                20151101050000.012345678


                And sorting is possible.



                [1] Values used to touch files used.

                touch -m -d '20151101 01:00:00.012345678-05' file1

                touch -a -d '20151101 01:00:00.987654321-05' file1

                touch -m -d '20151101 01:00:00.012345678-04' file2

                touch -a -d '20151101 01:00:00.987654321-04' file2






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The file modification time, could be formatted with date:



                  $ date -d @"$(stat -c %Y file1)" +'%Y%m%d%H%M%S'


                  Use %X for last access time and %Z for last change.



                  However, the maximum resolution of %X, %Y and %Z is seconds.

                  For nanoseconds resolution use the %x, %y and %z options:



                  $ date -d "$(stat -c %y file1)" +'%Y%m%d%H%M%S.%N'
                  20151101020000.012345678


                  As we are using date already, we can simplify by using date's -r option, or we can use find's formatted output:



                  $ date -r file1 +'%Y%m%d%H%M%S%z'
                  20151101010000-0500
                  $ find file1 -printf '%TY%Tm%Td%TH%TM%2.2TS%Tzn'
                  20151101010000-0500


                  The resolution of the above commands is seconds. If nanoseconds are needed:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
                  20151101010000.012345678-0500
                  $ find file1 -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
                  20151101010000.0123456780-0500


                  Those two commands give modification time

                  In date there is no way to get access or status time with the -r option.

                  In find: Change %T to %A for access time and to %C for status change time




                  About Time Zone



                  Please note that all the above commands are affected by the time zone used:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
                  20151101010000.012345678
                  $ TZ=GMT0 date -r file1 +'%Y%m%d%H%M%S.%N%z'
                  20151101060000.012345678


                  Note the Hour change from 01 to 06. That happens because the computer used had a time zone set for America/New_York. When the time is calculated at the GMT0 offset (aka GMT or GMT+0 or GMT-0) its value increase in 5 hours.



                  Also, the use of local time values (if incomplete) may present equal values when they are actually different:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N'
                  20151101010000.012345678
                  20151101010000.012345678


                  That's why the tailing %z is needed for local times:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N%z'; date -r file2 +'%Y%m%d%H%M%S.%N%z'
                  20151101010000.012345678-0500
                  20151101010000.012345678-0400


                  That makes the values different. But this kind of format makes sorting by time difficult. That is why the best solution is to use GMT times because the time offset (%z) is always 0 and can be omitted:



                  $ ( TZ=GMT0; date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N')
                  20151101060000.012345678
                  20151101050000.012345678


                  And sorting is possible.



                  [1] Values used to touch files used.

                  touch -m -d '20151101 01:00:00.012345678-05' file1

                  touch -a -d '20151101 01:00:00.987654321-05' file1

                  touch -m -d '20151101 01:00:00.012345678-04' file2

                  touch -a -d '20151101 01:00:00.987654321-04' file2






                  share|improve this answer














                  The file modification time, could be formatted with date:



                  $ date -d @"$(stat -c %Y file1)" +'%Y%m%d%H%M%S'


                  Use %X for last access time and %Z for last change.



                  However, the maximum resolution of %X, %Y and %Z is seconds.

                  For nanoseconds resolution use the %x, %y and %z options:



                  $ date -d "$(stat -c %y file1)" +'%Y%m%d%H%M%S.%N'
                  20151101020000.012345678


                  As we are using date already, we can simplify by using date's -r option, or we can use find's formatted output:



                  $ date -r file1 +'%Y%m%d%H%M%S%z'
                  20151101010000-0500
                  $ find file1 -printf '%TY%Tm%Td%TH%TM%2.2TS%Tzn'
                  20151101010000-0500


                  The resolution of the above commands is seconds. If nanoseconds are needed:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
                  20151101010000.012345678-0500
                  $ find file1 -printf '%TY%Tm%Td%TH%TM%TS%Tzn'
                  20151101010000.0123456780-0500


                  Those two commands give modification time

                  In date there is no way to get access or status time with the -r option.

                  In find: Change %T to %A for access time and to %C for status change time




                  About Time Zone



                  Please note that all the above commands are affected by the time zone used:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N%z'
                  20151101010000.012345678
                  $ TZ=GMT0 date -r file1 +'%Y%m%d%H%M%S.%N%z'
                  20151101060000.012345678


                  Note the Hour change from 01 to 06. That happens because the computer used had a time zone set for America/New_York. When the time is calculated at the GMT0 offset (aka GMT or GMT+0 or GMT-0) its value increase in 5 hours.



                  Also, the use of local time values (if incomplete) may present equal values when they are actually different:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N'
                  20151101010000.012345678
                  20151101010000.012345678


                  That's why the tailing %z is needed for local times:



                  $ date -r file1 +'%Y%m%d%H%M%S.%N%z'; date -r file2 +'%Y%m%d%H%M%S.%N%z'
                  20151101010000.012345678-0500
                  20151101010000.012345678-0400


                  That makes the values different. But this kind of format makes sorting by time difficult. That is why the best solution is to use GMT times because the time offset (%z) is always 0 and can be omitted:



                  $ ( TZ=GMT0; date -r file1 +'%Y%m%d%H%M%S.%N'; date -r file2 +'%Y%m%d%H%M%S.%N')
                  20151101060000.012345678
                  20151101050000.012345678


                  And sorting is possible.



                  [1] Values used to touch files used.

                  touch -m -d '20151101 01:00:00.012345678-05' file1

                  touch -a -d '20151101 01:00:00.987654321-05' file1

                  touch -m -d '20151101 01:00:00.012345678-04' file2

                  touch -a -d '20151101 01:00:00.987654321-04' file2







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 29 '16 at 22:05

























                  answered May 28 '16 at 8:45







                  user79743


































                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f285897%2fget-file-modification-time-in-a-specific-format-yyyymmddhh24miss%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)