Rename multiple files, add date in the middle












5















I want to rename all files in a folder, including a current time stamp in the name, retaining the original extension, in Solaris, like this:



test1.txt > test1.date.txt



I tried this, but I lose the first part of the name:



find * -prune -type f -name '*.txt' -exec mv {} {}.$(date +'%Y%m%d%H%M').txt ;


What am I missing ?










share|improve this question





























    5















    I want to rename all files in a folder, including a current time stamp in the name, retaining the original extension, in Solaris, like this:



    test1.txt > test1.date.txt



    I tried this, but I lose the first part of the name:



    find * -prune -type f -name '*.txt' -exec mv {} {}.$(date +'%Y%m%d%H%M').txt ;


    What am I missing ?










    share|improve this question



























      5












      5








      5








      I want to rename all files in a folder, including a current time stamp in the name, retaining the original extension, in Solaris, like this:



      test1.txt > test1.date.txt



      I tried this, but I lose the first part of the name:



      find * -prune -type f -name '*.txt' -exec mv {} {}.$(date +'%Y%m%d%H%M').txt ;


      What am I missing ?










      share|improve this question
















      I want to rename all files in a folder, including a current time stamp in the name, retaining the original extension, in Solaris, like this:



      test1.txt > test1.date.txt



      I tried this, but I lose the first part of the name:



      find * -prune -type f -name '*.txt' -exec mv {} {}.$(date +'%Y%m%d%H%M').txt ;


      What am I missing ?







      bash shell-script find solaris mv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 25 at 16:13









      K7AAY

      525521




      525521










      asked Jan 25 at 16:02









      Vladimir ZaguzinVladimir Zaguzin

      284




      284






















          2 Answers
          2






          active

          oldest

          votes


















          8
















          find . -type f -name '*.txt' -exec sh -c '
          now=$( date +%Y%m%d%H%M )
          for pathname do
          mv "$pathname" "${pathname%.txt}.$now.txt"
          done' sh {} +


          This would find all pathnames of regular files in or below the current directory, whose names end in .txt. For batches of these, a short shell script is called. This script will first get the timestamp (this is had only once for each batch, for efficiency) and then loop over the given pathnames.



          For each pathname, it will rename it by removing the final .txt (using ${pathname%.txt}), and adding a dot, the timestamp and .txt.



          This has not been tested on Solaris, but uses only standard components and options etc.



          To compute the timestamp only once, before calling find, use



          now=$( date +%Y%m%d%H%M ) 
          find ...as before (but without assigning to now)...


          or



          env now="$( date +%Y%m%d%H%M )" 
          find ...as before (but without assigning to now)...


          (note the line continuations in the above two commands)



          If the files are located in a single directory, just run the loop:



          now=$( date +%Y%m%d%H%M )
          for pathname in ./*.txt; do
          mv "$pathname" "${pathname%.txt}.$now.txt"
          done


          This assumes that the pattern ./*.txt matches all names that you'd like to rename.



          Related:




          • Understanding the -exec option of `find`




          Your {}.$(date +'%Y%m%d%H%M').txt does not work portably. It may work with some implementations of find, but an implementation is not required to expand {} to the current pathname if the {} occurs together with another string.



          The relevant text from the POSIX standard is:




          A utility_name or argument containing only the two characters {} shall be replaced by the current pathname. If a utility_name or argument string contains the two characters {}, but not just the two characters {}, it is implementation-defined whether find replaces those two characters or uses the string without change.







          share|improve this answer


























          • You might want mv -- fname ... in case the names start with hyphens

            – D. Ben Knoble
            Jan 25 at 22:49






          • 2





            @D.BenKnoble They won't start with dashes. find will add the search path (at least ./) to each pathname, and the loop alternative will also always have ./ before any filename due to the way I wrote the pattern.

            – Kusalananda
            Jan 25 at 22:56



















          3














          An alternative, though am not sure how solaris compatible it is....



          from=txt
          to=$(date +%Y%m%d%H%M).txt
          find * -prune -type f -name "*.$from" -exec sh -c 'mv "$3" "${3%.$1}.$2"' sh "$from" "$to" {} ';'


          Stolen shamelessly from the unix.stackexhange link given by @Kulasananda but I like the way that the call from sh composes the variables for sh -c mv.



          Sweet.






          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',
            autoActivateHeartbeat: false,
            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%2f496706%2frename-multiple-files-add-date-in-the-middle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            8
















            find . -type f -name '*.txt' -exec sh -c '
            now=$( date +%Y%m%d%H%M )
            for pathname do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done' sh {} +


            This would find all pathnames of regular files in or below the current directory, whose names end in .txt. For batches of these, a short shell script is called. This script will first get the timestamp (this is had only once for each batch, for efficiency) and then loop over the given pathnames.



            For each pathname, it will rename it by removing the final .txt (using ${pathname%.txt}), and adding a dot, the timestamp and .txt.



            This has not been tested on Solaris, but uses only standard components and options etc.



            To compute the timestamp only once, before calling find, use



            now=$( date +%Y%m%d%H%M ) 
            find ...as before (but without assigning to now)...


            or



            env now="$( date +%Y%m%d%H%M )" 
            find ...as before (but without assigning to now)...


            (note the line continuations in the above two commands)



            If the files are located in a single directory, just run the loop:



            now=$( date +%Y%m%d%H%M )
            for pathname in ./*.txt; do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done


            This assumes that the pattern ./*.txt matches all names that you'd like to rename.



            Related:




            • Understanding the -exec option of `find`




            Your {}.$(date +'%Y%m%d%H%M').txt does not work portably. It may work with some implementations of find, but an implementation is not required to expand {} to the current pathname if the {} occurs together with another string.



            The relevant text from the POSIX standard is:




            A utility_name or argument containing only the two characters {} shall be replaced by the current pathname. If a utility_name or argument string contains the two characters {}, but not just the two characters {}, it is implementation-defined whether find replaces those two characters or uses the string without change.







            share|improve this answer


























            • You might want mv -- fname ... in case the names start with hyphens

              – D. Ben Knoble
              Jan 25 at 22:49






            • 2





              @D.BenKnoble They won't start with dashes. find will add the search path (at least ./) to each pathname, and the loop alternative will also always have ./ before any filename due to the way I wrote the pattern.

              – Kusalananda
              Jan 25 at 22:56
















            8
















            find . -type f -name '*.txt' -exec sh -c '
            now=$( date +%Y%m%d%H%M )
            for pathname do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done' sh {} +


            This would find all pathnames of regular files in or below the current directory, whose names end in .txt. For batches of these, a short shell script is called. This script will first get the timestamp (this is had only once for each batch, for efficiency) and then loop over the given pathnames.



            For each pathname, it will rename it by removing the final .txt (using ${pathname%.txt}), and adding a dot, the timestamp and .txt.



            This has not been tested on Solaris, but uses only standard components and options etc.



            To compute the timestamp only once, before calling find, use



            now=$( date +%Y%m%d%H%M ) 
            find ...as before (but without assigning to now)...


            or



            env now="$( date +%Y%m%d%H%M )" 
            find ...as before (but without assigning to now)...


            (note the line continuations in the above two commands)



            If the files are located in a single directory, just run the loop:



            now=$( date +%Y%m%d%H%M )
            for pathname in ./*.txt; do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done


            This assumes that the pattern ./*.txt matches all names that you'd like to rename.



            Related:




            • Understanding the -exec option of `find`




            Your {}.$(date +'%Y%m%d%H%M').txt does not work portably. It may work with some implementations of find, but an implementation is not required to expand {} to the current pathname if the {} occurs together with another string.



            The relevant text from the POSIX standard is:




            A utility_name or argument containing only the two characters {} shall be replaced by the current pathname. If a utility_name or argument string contains the two characters {}, but not just the two characters {}, it is implementation-defined whether find replaces those two characters or uses the string without change.







            share|improve this answer


























            • You might want mv -- fname ... in case the names start with hyphens

              – D. Ben Knoble
              Jan 25 at 22:49






            • 2





              @D.BenKnoble They won't start with dashes. find will add the search path (at least ./) to each pathname, and the loop alternative will also always have ./ before any filename due to the way I wrote the pattern.

              – Kusalananda
              Jan 25 at 22:56














            8












            8








            8









            find . -type f -name '*.txt' -exec sh -c '
            now=$( date +%Y%m%d%H%M )
            for pathname do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done' sh {} +


            This would find all pathnames of regular files in or below the current directory, whose names end in .txt. For batches of these, a short shell script is called. This script will first get the timestamp (this is had only once for each batch, for efficiency) and then loop over the given pathnames.



            For each pathname, it will rename it by removing the final .txt (using ${pathname%.txt}), and adding a dot, the timestamp and .txt.



            This has not been tested on Solaris, but uses only standard components and options etc.



            To compute the timestamp only once, before calling find, use



            now=$( date +%Y%m%d%H%M ) 
            find ...as before (but without assigning to now)...


            or



            env now="$( date +%Y%m%d%H%M )" 
            find ...as before (but without assigning to now)...


            (note the line continuations in the above two commands)



            If the files are located in a single directory, just run the loop:



            now=$( date +%Y%m%d%H%M )
            for pathname in ./*.txt; do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done


            This assumes that the pattern ./*.txt matches all names that you'd like to rename.



            Related:




            • Understanding the -exec option of `find`




            Your {}.$(date +'%Y%m%d%H%M').txt does not work portably. It may work with some implementations of find, but an implementation is not required to expand {} to the current pathname if the {} occurs together with another string.



            The relevant text from the POSIX standard is:




            A utility_name or argument containing only the two characters {} shall be replaced by the current pathname. If a utility_name or argument string contains the two characters {}, but not just the two characters {}, it is implementation-defined whether find replaces those two characters or uses the string without change.







            share|improve this answer

















            find . -type f -name '*.txt' -exec sh -c '
            now=$( date +%Y%m%d%H%M )
            for pathname do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done' sh {} +


            This would find all pathnames of regular files in or below the current directory, whose names end in .txt. For batches of these, a short shell script is called. This script will first get the timestamp (this is had only once for each batch, for efficiency) and then loop over the given pathnames.



            For each pathname, it will rename it by removing the final .txt (using ${pathname%.txt}), and adding a dot, the timestamp and .txt.



            This has not been tested on Solaris, but uses only standard components and options etc.



            To compute the timestamp only once, before calling find, use



            now=$( date +%Y%m%d%H%M ) 
            find ...as before (but without assigning to now)...


            or



            env now="$( date +%Y%m%d%H%M )" 
            find ...as before (but without assigning to now)...


            (note the line continuations in the above two commands)



            If the files are located in a single directory, just run the loop:



            now=$( date +%Y%m%d%H%M )
            for pathname in ./*.txt; do
            mv "$pathname" "${pathname%.txt}.$now.txt"
            done


            This assumes that the pattern ./*.txt matches all names that you'd like to rename.



            Related:




            • Understanding the -exec option of `find`




            Your {}.$(date +'%Y%m%d%H%M').txt does not work portably. It may work with some implementations of find, but an implementation is not required to expand {} to the current pathname if the {} occurs together with another string.



            The relevant text from the POSIX standard is:




            A utility_name or argument containing only the two characters {} shall be replaced by the current pathname. If a utility_name or argument string contains the two characters {}, but not just the two characters {}, it is implementation-defined whether find replaces those two characters or uses the string without change.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 25 at 20:28

























            answered Jan 25 at 16:56









            KusalanandaKusalananda

            127k16240395




            127k16240395













            • You might want mv -- fname ... in case the names start with hyphens

              – D. Ben Knoble
              Jan 25 at 22:49






            • 2





              @D.BenKnoble They won't start with dashes. find will add the search path (at least ./) to each pathname, and the loop alternative will also always have ./ before any filename due to the way I wrote the pattern.

              – Kusalananda
              Jan 25 at 22:56



















            • You might want mv -- fname ... in case the names start with hyphens

              – D. Ben Knoble
              Jan 25 at 22:49






            • 2





              @D.BenKnoble They won't start with dashes. find will add the search path (at least ./) to each pathname, and the loop alternative will also always have ./ before any filename due to the way I wrote the pattern.

              – Kusalananda
              Jan 25 at 22:56

















            You might want mv -- fname ... in case the names start with hyphens

            – D. Ben Knoble
            Jan 25 at 22:49





            You might want mv -- fname ... in case the names start with hyphens

            – D. Ben Knoble
            Jan 25 at 22:49




            2




            2





            @D.BenKnoble They won't start with dashes. find will add the search path (at least ./) to each pathname, and the loop alternative will also always have ./ before any filename due to the way I wrote the pattern.

            – Kusalananda
            Jan 25 at 22:56





            @D.BenKnoble They won't start with dashes. find will add the search path (at least ./) to each pathname, and the loop alternative will also always have ./ before any filename due to the way I wrote the pattern.

            – Kusalananda
            Jan 25 at 22:56













            3














            An alternative, though am not sure how solaris compatible it is....



            from=txt
            to=$(date +%Y%m%d%H%M).txt
            find * -prune -type f -name "*.$from" -exec sh -c 'mv "$3" "${3%.$1}.$2"' sh "$from" "$to" {} ';'


            Stolen shamelessly from the unix.stackexhange link given by @Kulasananda but I like the way that the call from sh composes the variables for sh -c mv.



            Sweet.






            share|improve this answer




























              3














              An alternative, though am not sure how solaris compatible it is....



              from=txt
              to=$(date +%Y%m%d%H%M).txt
              find * -prune -type f -name "*.$from" -exec sh -c 'mv "$3" "${3%.$1}.$2"' sh "$from" "$to" {} ';'


              Stolen shamelessly from the unix.stackexhange link given by @Kulasananda but I like the way that the call from sh composes the variables for sh -c mv.



              Sweet.






              share|improve this answer


























                3












                3








                3







                An alternative, though am not sure how solaris compatible it is....



                from=txt
                to=$(date +%Y%m%d%H%M).txt
                find * -prune -type f -name "*.$from" -exec sh -c 'mv "$3" "${3%.$1}.$2"' sh "$from" "$to" {} ';'


                Stolen shamelessly from the unix.stackexhange link given by @Kulasananda but I like the way that the call from sh composes the variables for sh -c mv.



                Sweet.






                share|improve this answer













                An alternative, though am not sure how solaris compatible it is....



                from=txt
                to=$(date +%Y%m%d%H%M).txt
                find * -prune -type f -name "*.$from" -exec sh -c 'mv "$3" "${3%.$1}.$2"' sh "$from" "$to" {} ';'


                Stolen shamelessly from the unix.stackexhange link given by @Kulasananda but I like the way that the call from sh composes the variables for sh -c mv.



                Sweet.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 25 at 17:46









                bu5hmanbu5hman

                1,282214




                1,282214






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f496706%2frename-multiple-files-add-date-in-the-middle%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

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?