Bulk file rename, maintaining timestamps, excluding some












0















In Ubuntu 18.10 have beeen using mmv to prepend folder names (a date) to file names within multiple folders ('-n' flag here to review results):



mmv -n './????-??-??*/*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-#10.#11'


which for example converts:



./2018-12-11/DSC05287.ARW -> ./2018-12-11/20181211-DSC05287.ARW


It also converts:



./2018-12-11/20181211-DSC05287.ARW -> ./2018-12-11/20181211-20181211-DSC05287.ARW


whilst maintaining the timestamps.



Some files have already been renamed, some have not.



How can I filter the command to exclude files matching a pattern - those which start with an 8-digit number followed by a hyphen?



Or do I need a different approach?










share|improve this question























  • Could it be as simple as making the leading DSC substring explicit? i.e. mmv -n './????-??-??*/DSC*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-DSC#10.#11'

    – steeldriver
    Feb 11 at 16:37











  • That'd be a possibility except there are a whole range of filenames, so DSC* includes a proportion. But possibly I could do it folder by folder.

    – user118684
    Feb 11 at 19:41


















0















In Ubuntu 18.10 have beeen using mmv to prepend folder names (a date) to file names within multiple folders ('-n' flag here to review results):



mmv -n './????-??-??*/*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-#10.#11'


which for example converts:



./2018-12-11/DSC05287.ARW -> ./2018-12-11/20181211-DSC05287.ARW


It also converts:



./2018-12-11/20181211-DSC05287.ARW -> ./2018-12-11/20181211-20181211-DSC05287.ARW


whilst maintaining the timestamps.



Some files have already been renamed, some have not.



How can I filter the command to exclude files matching a pattern - those which start with an 8-digit number followed by a hyphen?



Or do I need a different approach?










share|improve this question























  • Could it be as simple as making the leading DSC substring explicit? i.e. mmv -n './????-??-??*/DSC*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-DSC#10.#11'

    – steeldriver
    Feb 11 at 16:37











  • That'd be a possibility except there are a whole range of filenames, so DSC* includes a proportion. But possibly I could do it folder by folder.

    – user118684
    Feb 11 at 19:41
















0












0








0








In Ubuntu 18.10 have beeen using mmv to prepend folder names (a date) to file names within multiple folders ('-n' flag here to review results):



mmv -n './????-??-??*/*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-#10.#11'


which for example converts:



./2018-12-11/DSC05287.ARW -> ./2018-12-11/20181211-DSC05287.ARW


It also converts:



./2018-12-11/20181211-DSC05287.ARW -> ./2018-12-11/20181211-20181211-DSC05287.ARW


whilst maintaining the timestamps.



Some files have already been renamed, some have not.



How can I filter the command to exclude files matching a pattern - those which start with an 8-digit number followed by a hyphen?



Or do I need a different approach?










share|improve this question














In Ubuntu 18.10 have beeen using mmv to prepend folder names (a date) to file names within multiple folders ('-n' flag here to review results):



mmv -n './????-??-??*/*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-#10.#11'


which for example converts:



./2018-12-11/DSC05287.ARW -> ./2018-12-11/20181211-DSC05287.ARW


It also converts:



./2018-12-11/20181211-DSC05287.ARW -> ./2018-12-11/20181211-20181211-DSC05287.ARW


whilst maintaining the timestamps.



Some files have already been renamed, some have not.



How can I filter the command to exclude files matching a pattern - those which start with an 8-digit number followed by a hyphen?



Or do I need a different approach?







rename timestamp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 11 at 15:04









user118684user118684

6618




6618













  • Could it be as simple as making the leading DSC substring explicit? i.e. mmv -n './????-??-??*/DSC*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-DSC#10.#11'

    – steeldriver
    Feb 11 at 16:37











  • That'd be a possibility except there are a whole range of filenames, so DSC* includes a proportion. But possibly I could do it folder by folder.

    – user118684
    Feb 11 at 19:41





















  • Could it be as simple as making the leading DSC substring explicit? i.e. mmv -n './????-??-??*/DSC*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-DSC#10.#11'

    – steeldriver
    Feb 11 at 16:37











  • That'd be a possibility except there are a whole range of filenames, so DSC* includes a proportion. But possibly I could do it folder by folder.

    – user118684
    Feb 11 at 19:41



















Could it be as simple as making the leading DSC substring explicit? i.e. mmv -n './????-??-??*/DSC*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-DSC#10.#11'

– steeldriver
Feb 11 at 16:37





Could it be as simple as making the leading DSC substring explicit? i.e. mmv -n './????-??-??*/DSC*.*' './#1#2#3#4-#5#6-#7#8#9/#1#2#3#4#5#6#7#8-DSC#10.#11'

– steeldriver
Feb 11 at 16:37













That'd be a possibility except there are a whole range of filenames, so DSC* includes a proportion. But possibly I could do it folder by folder.

– user118684
Feb 11 at 19:41







That'd be a possibility except there are a whole range of filenames, so DSC* includes a proportion. But possibly I could do it folder by folder.

– user118684
Feb 11 at 19:41












1 Answer
1






active

oldest

votes


















2














I'm not a fan of all-in-one renaming utilities, so here's how I would do it using only "standard (GNU) equipment", specifically bash, find and mv.



Assuming you're only interested in files that are one directory level down (so find -maxdepth 2), and specifically ignoring files that have already been previously renamed (but doing a sanity check with regex backreferences to avoid making assumptions):



find -regextype egrep -maxdepth 2 -type f ! -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-.+' | while read f; do

if [[ $f =~ ^(./([0-9]{4}-[0-9]{2}-[0-9]{2}).*)/(.+)$ ]]; then
# The above regex groups its matches into:
# ${BASH_REMATCH[1]} = the dir pathname
# ${BASH_REMATCH[2]} = the date in the dir pathname (remember to strip its dashes)
# ${BASH_REMATCH[3]} = the file name

mv -v "$f" "${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]//-}-${BASH_REMATCH[3]}"

fi

done


And to fix the ones that have already been incorrectly "double-dated" (again, using backreferences to avoid making unwarranted assumptions):



find -regextype egrep -maxdepth 2 -type f -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-123-.+' | while read f; do

if [[ $f =~ ^(./.+)/([0-9]{8})-[0-9]{8}-(.+)$ ]]; then
# The above regex groups its matches into:
# ${BASH_REMATCH[1]} = the dir pathname
# ${BASH_REMATCH[2]} = the first date chunk
# ${BASH_REMATCH[3]} = the filename "tail"

mv -v "$f" "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}-${BASH_REMATCH[3]}"

fi

done


Further Reading:





  • Regular-Expressions.info: in case you need help with regexes


  • bash(1) man page: search for the =~ operator, then read up on the special BASH_REMATCH array


  • findutils | Regular Expressions: details on the surprising variety of regexes supported by GNU find and locate.






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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1117389%2fbulk-file-rename-maintaining-timestamps-excluding-some%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    I'm not a fan of all-in-one renaming utilities, so here's how I would do it using only "standard (GNU) equipment", specifically bash, find and mv.



    Assuming you're only interested in files that are one directory level down (so find -maxdepth 2), and specifically ignoring files that have already been previously renamed (but doing a sanity check with regex backreferences to avoid making assumptions):



    find -regextype egrep -maxdepth 2 -type f ! -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-.+' | while read f; do

    if [[ $f =~ ^(./([0-9]{4}-[0-9]{2}-[0-9]{2}).*)/(.+)$ ]]; then
    # The above regex groups its matches into:
    # ${BASH_REMATCH[1]} = the dir pathname
    # ${BASH_REMATCH[2]} = the date in the dir pathname (remember to strip its dashes)
    # ${BASH_REMATCH[3]} = the file name

    mv -v "$f" "${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]//-}-${BASH_REMATCH[3]}"

    fi

    done


    And to fix the ones that have already been incorrectly "double-dated" (again, using backreferences to avoid making unwarranted assumptions):



    find -regextype egrep -maxdepth 2 -type f -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-123-.+' | while read f; do

    if [[ $f =~ ^(./.+)/([0-9]{8})-[0-9]{8}-(.+)$ ]]; then
    # The above regex groups its matches into:
    # ${BASH_REMATCH[1]} = the dir pathname
    # ${BASH_REMATCH[2]} = the first date chunk
    # ${BASH_REMATCH[3]} = the filename "tail"

    mv -v "$f" "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}-${BASH_REMATCH[3]}"

    fi

    done


    Further Reading:





    • Regular-Expressions.info: in case you need help with regexes


    • bash(1) man page: search for the =~ operator, then read up on the special BASH_REMATCH array


    • findutils | Regular Expressions: details on the surprising variety of regexes supported by GNU find and locate.






    share|improve this answer




























      2














      I'm not a fan of all-in-one renaming utilities, so here's how I would do it using only "standard (GNU) equipment", specifically bash, find and mv.



      Assuming you're only interested in files that are one directory level down (so find -maxdepth 2), and specifically ignoring files that have already been previously renamed (but doing a sanity check with regex backreferences to avoid making assumptions):



      find -regextype egrep -maxdepth 2 -type f ! -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-.+' | while read f; do

      if [[ $f =~ ^(./([0-9]{4}-[0-9]{2}-[0-9]{2}).*)/(.+)$ ]]; then
      # The above regex groups its matches into:
      # ${BASH_REMATCH[1]} = the dir pathname
      # ${BASH_REMATCH[2]} = the date in the dir pathname (remember to strip its dashes)
      # ${BASH_REMATCH[3]} = the file name

      mv -v "$f" "${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]//-}-${BASH_REMATCH[3]}"

      fi

      done


      And to fix the ones that have already been incorrectly "double-dated" (again, using backreferences to avoid making unwarranted assumptions):



      find -regextype egrep -maxdepth 2 -type f -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-123-.+' | while read f; do

      if [[ $f =~ ^(./.+)/([0-9]{8})-[0-9]{8}-(.+)$ ]]; then
      # The above regex groups its matches into:
      # ${BASH_REMATCH[1]} = the dir pathname
      # ${BASH_REMATCH[2]} = the first date chunk
      # ${BASH_REMATCH[3]} = the filename "tail"

      mv -v "$f" "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}-${BASH_REMATCH[3]}"

      fi

      done


      Further Reading:





      • Regular-Expressions.info: in case you need help with regexes


      • bash(1) man page: search for the =~ operator, then read up on the special BASH_REMATCH array


      • findutils | Regular Expressions: details on the surprising variety of regexes supported by GNU find and locate.






      share|improve this answer


























        2












        2








        2







        I'm not a fan of all-in-one renaming utilities, so here's how I would do it using only "standard (GNU) equipment", specifically bash, find and mv.



        Assuming you're only interested in files that are one directory level down (so find -maxdepth 2), and specifically ignoring files that have already been previously renamed (but doing a sanity check with regex backreferences to avoid making assumptions):



        find -regextype egrep -maxdepth 2 -type f ! -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-.+' | while read f; do

        if [[ $f =~ ^(./([0-9]{4}-[0-9]{2}-[0-9]{2}).*)/(.+)$ ]]; then
        # The above regex groups its matches into:
        # ${BASH_REMATCH[1]} = the dir pathname
        # ${BASH_REMATCH[2]} = the date in the dir pathname (remember to strip its dashes)
        # ${BASH_REMATCH[3]} = the file name

        mv -v "$f" "${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]//-}-${BASH_REMATCH[3]}"

        fi

        done


        And to fix the ones that have already been incorrectly "double-dated" (again, using backreferences to avoid making unwarranted assumptions):



        find -regextype egrep -maxdepth 2 -type f -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-123-.+' | while read f; do

        if [[ $f =~ ^(./.+)/([0-9]{8})-[0-9]{8}-(.+)$ ]]; then
        # The above regex groups its matches into:
        # ${BASH_REMATCH[1]} = the dir pathname
        # ${BASH_REMATCH[2]} = the first date chunk
        # ${BASH_REMATCH[3]} = the filename "tail"

        mv -v "$f" "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}-${BASH_REMATCH[3]}"

        fi

        done


        Further Reading:





        • Regular-Expressions.info: in case you need help with regexes


        • bash(1) man page: search for the =~ operator, then read up on the special BASH_REMATCH array


        • findutils | Regular Expressions: details on the surprising variety of regexes supported by GNU find and locate.






        share|improve this answer













        I'm not a fan of all-in-one renaming utilities, so here's how I would do it using only "standard (GNU) equipment", specifically bash, find and mv.



        Assuming you're only interested in files that are one directory level down (so find -maxdepth 2), and specifically ignoring files that have already been previously renamed (but doing a sanity check with regex backreferences to avoid making assumptions):



        find -regextype egrep -maxdepth 2 -type f ! -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-.+' | while read f; do

        if [[ $f =~ ^(./([0-9]{4}-[0-9]{2}-[0-9]{2}).*)/(.+)$ ]]; then
        # The above regex groups its matches into:
        # ${BASH_REMATCH[1]} = the dir pathname
        # ${BASH_REMATCH[2]} = the date in the dir pathname (remember to strip its dashes)
        # ${BASH_REMATCH[3]} = the file name

        mv -v "$f" "${BASH_REMATCH[1]}"/"${BASH_REMATCH[2]//-}-${BASH_REMATCH[3]}"

        fi

        done


        And to fix the ones that have already been incorrectly "double-dated" (again, using backreferences to avoid making unwarranted assumptions):



        find -regextype egrep -maxdepth 2 -type f -regex './([0-9]{4})-([0-9]{2})-([0-9]{2}).*/123-123-.+' | while read f; do

        if [[ $f =~ ^(./.+)/([0-9]{8})-[0-9]{8}-(.+)$ ]]; then
        # The above regex groups its matches into:
        # ${BASH_REMATCH[1]} = the dir pathname
        # ${BASH_REMATCH[2]} = the first date chunk
        # ${BASH_REMATCH[3]} = the filename "tail"

        mv -v "$f" "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}-${BASH_REMATCH[3]}"

        fi

        done


        Further Reading:





        • Regular-Expressions.info: in case you need help with regexes


        • bash(1) man page: search for the =~ operator, then read up on the special BASH_REMATCH array


        • findutils | Regular Expressions: details on the surprising variety of regexes supported by GNU find and locate.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 11 at 16:39









        AdrianAdrian

        1563




        1563






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1117389%2fbulk-file-rename-maintaining-timestamps-excluding-some%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mouse cursor on multiple screens with different PPI

            Agildo Ribeiro

            Sometime when accessing a menu: “Ubuntu 16.04 has experienced an internal error”