Script to change the timestamp of files according to filename

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











up vote
-1
down vote

favorite












I'm in a macOS environment.



I'd like to change the time stamp on multiple files that has names that starts with this format:



XXXXX_DD_MM_YYYY_HH-MM-SS_


This is an example of a filename:



CALLU_16-10-2018_10-58-26_p123456789012.mp3


Creation and modification date should be change to 16/10/2018 at 10:58:26.



I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP:



for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done









share|improve this question









New contributor




Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
    – Giancarlo Passaglia
    yesterday










  • find . -iname 'XXXX*' | xargs touch
    – user1133275
    yesterday










  • Try removing the 2>/dev/null to see a useful error message, possibly including the output of the awk step.
    – JigglyNaga
    11 hours ago










  • this should work, provided that the XXXXX doesn't contain - or _: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3. I don't have any MacOS to test on, though.
    – mosvy
    11 hours ago















up vote
-1
down vote

favorite












I'm in a macOS environment.



I'd like to change the time stamp on multiple files that has names that starts with this format:



XXXXX_DD_MM_YYYY_HH-MM-SS_


This is an example of a filename:



CALLU_16-10-2018_10-58-26_p123456789012.mp3


Creation and modification date should be change to 16/10/2018 at 10:58:26.



I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP:



for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done









share|improve this question









New contributor




Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
    – Giancarlo Passaglia
    yesterday










  • find . -iname 'XXXX*' | xargs touch
    – user1133275
    yesterday










  • Try removing the 2>/dev/null to see a useful error message, possibly including the output of the awk step.
    – JigglyNaga
    11 hours ago










  • this should work, provided that the XXXXX doesn't contain - or _: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3. I don't have any MacOS to test on, though.
    – mosvy
    11 hours ago













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm in a macOS environment.



I'd like to change the time stamp on multiple files that has names that starts with this format:



XXXXX_DD_MM_YYYY_HH-MM-SS_


This is an example of a filename:



CALLU_16-10-2018_10-58-26_p123456789012.mp3


Creation and modification date should be change to 16/10/2018 at 10:58:26.



I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP:



for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done









share|improve this question









New contributor




Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm in a macOS environment.



I'd like to change the time stamp on multiple files that has names that starts with this format:



XXXXX_DD_MM_YYYY_HH-MM-SS_


This is an example of a filename:



CALLU_16-10-2018_10-58-26_p123456789012.mp3


Creation and modification date should be change to 16/10/2018 at 10:58:26.



I had the following script for filenames of the form Call@1234567890(1234567890)_20160624205913. It extracts date and time from the end of the file name, as a parameter for touch -t STAMP:



for f in *; do
t="$(awk '/_.*/ match($0, /_.*/)
print substr($0, RSTART + 1, RLENGTH - 7)
'<<<"$f" )"
touch -t "$t" "$f" 2>/dev/null
done






scripting osx timestamps






share|improve this question









New contributor




Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 11 hours ago









JigglyNaga

3,424728




3,424728






New contributor




Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Giancarlo Passaglia

11




11




New contributor




Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Giancarlo Passaglia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
    – Giancarlo Passaglia
    yesterday










  • find . -iname 'XXXX*' | xargs touch
    – user1133275
    yesterday










  • Try removing the 2>/dev/null to see a useful error message, possibly including the output of the awk step.
    – JigglyNaga
    11 hours ago










  • this should work, provided that the XXXXX doesn't contain - or _: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3. I don't have any MacOS to test on, though.
    – mosvy
    11 hours ago

















  • I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
    – Giancarlo Passaglia
    yesterday










  • find . -iname 'XXXX*' | xargs touch
    – user1133275
    yesterday










  • Try removing the 2>/dev/null to see a useful error message, possibly including the output of the awk step.
    – JigglyNaga
    11 hours ago










  • this should work, provided that the XXXXX doesn't contain - or _: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3. I don't have any MacOS to test on, though.
    – mosvy
    11 hours ago
















I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday




I'd like to change the attribute of the file itself. The meta data is necessary to be with date of modify, creation etc. with da information of the file named in that format.
– Giancarlo Passaglia
yesterday












find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday




find . -iname 'XXXX*' | xargs touch
– user1133275
yesterday












Try removing the 2>/dev/null to see a useful error message, possibly including the output of the awk step.
– JigglyNaga
11 hours ago




Try removing the 2>/dev/null to see a useful error message, possibly including the output of the awk step.
– JigglyNaga
11 hours ago












this should work, provided that the XXXXX doesn't contain - or _: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3. I don't have any MacOS to test on, though.
– mosvy
11 hours ago





this should work, provided that the XXXXX doesn't contain - or _: xtouch() (set -f; IFS='_-'; f=$1; set -- $f; touch -t "$4$3$2$5$6.$7" "$f") ; xtouch CALLU_16-10-2018_10-58-26_p123456789012.mp3. I don't have any MacOS to test on, though.
– mosvy
11 hours ago











1 Answer
1






active

oldest

votes

















up vote
0
down vote













In your filename, the date is in little-endian format, so your awk script will need to swap the components, as well as removing all the separators:



awk -F '[-_]' 'print $4$3$2$5$6"."$7'


(If your input filenames contain additional - characters before the date, this will need rewriting.)



Test this with your example file:



$ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
201810161058.26


So your updated script could look something like:



for f in *.mp3; do
t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
touch -t "$t" "$f"
done


Your old awk script was searching for the first _ in the filename, and then using everything from the next character (RSTART+1) to 6 characters before the end (RLENGTH-7: +1-7 = -6).



On your input filename, that would produce 20160624. touch expects the timestamp to be in the form:




[[CC]YY]MMDDhhmm[.SS]




...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch reporting an error because of the 2>/dev/null.






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
    );



    );






    Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481538%2fscript-to-change-the-timestamp-of-files-according-to-filename%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    In your filename, the date is in little-endian format, so your awk script will need to swap the components, as well as removing all the separators:



    awk -F '[-_]' 'print $4$3$2$5$6"."$7'


    (If your input filenames contain additional - characters before the date, this will need rewriting.)



    Test this with your example file:



    $ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
    201810161058.26


    So your updated script could look something like:



    for f in *.mp3; do
    t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
    touch -t "$t" "$f"
    done


    Your old awk script was searching for the first _ in the filename, and then using everything from the next character (RSTART+1) to 6 characters before the end (RLENGTH-7: +1-7 = -6).



    On your input filename, that would produce 20160624. touch expects the timestamp to be in the form:




    [[CC]YY]MMDDhhmm[.SS]




    ...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch reporting an error because of the 2>/dev/null.






    share|improve this answer
























      up vote
      0
      down vote













      In your filename, the date is in little-endian format, so your awk script will need to swap the components, as well as removing all the separators:



      awk -F '[-_]' 'print $4$3$2$5$6"."$7'


      (If your input filenames contain additional - characters before the date, this will need rewriting.)



      Test this with your example file:



      $ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
      201810161058.26


      So your updated script could look something like:



      for f in *.mp3; do
      t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
      touch -t "$t" "$f"
      done


      Your old awk script was searching for the first _ in the filename, and then using everything from the next character (RSTART+1) to 6 characters before the end (RLENGTH-7: +1-7 = -6).



      On your input filename, that would produce 20160624. touch expects the timestamp to be in the form:




      [[CC]YY]MMDDhhmm[.SS]




      ...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch reporting an error because of the 2>/dev/null.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        In your filename, the date is in little-endian format, so your awk script will need to swap the components, as well as removing all the separators:



        awk -F '[-_]' 'print $4$3$2$5$6"."$7'


        (If your input filenames contain additional - characters before the date, this will need rewriting.)



        Test this with your example file:



        $ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
        201810161058.26


        So your updated script could look something like:



        for f in *.mp3; do
        t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
        touch -t "$t" "$f"
        done


        Your old awk script was searching for the first _ in the filename, and then using everything from the next character (RSTART+1) to 6 characters before the end (RLENGTH-7: +1-7 = -6).



        On your input filename, that would produce 20160624. touch expects the timestamp to be in the form:




        [[CC]YY]MMDDhhmm[.SS]




        ...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch reporting an error because of the 2>/dev/null.






        share|improve this answer












        In your filename, the date is in little-endian format, so your awk script will need to swap the components, as well as removing all the separators:



        awk -F '[-_]' 'print $4$3$2$5$6"."$7'


        (If your input filenames contain additional - characters before the date, this will need rewriting.)



        Test this with your example file:



        $ echo 'CALLU_16-10-2018_10-58-26_p123456789012.mp3' | awk -F '[-_]' 'print $4$3$2$5$6"."$7'
        201810161058.26


        So your updated script could look something like:



        for f in *.mp3; do
        t=$(echo "$f" | awk -F '[-_]' 'print $4$3$2$5$6"."$7')
        touch -t "$t" "$f"
        done


        Your old awk script was searching for the first _ in the filename, and then using everything from the next character (RSTART+1) to 6 characters before the end (RLENGTH-7: +1-7 = -6).



        On your input filename, that would produce 20160624. touch expects the timestamp to be in the form:




        [[CC]YY]MMDDhhmm[.SS]




        ...which would mean, with only 8 digits, the "20" is interpreted as the month, ie. an invalid date. But you wouldn't see touch reporting an error because of the 2>/dev/null.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        JigglyNaga

        3,424728




        3,424728




















            Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.












            Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.











            Giancarlo Passaglia is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481538%2fscript-to-change-the-timestamp-of-files-according-to-filename%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