How to batch cp files and rename by adding prefix in current directory in one command-line without using for loop?

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











up vote
1
down vote

favorite












Example:



.
├── f1.md
├── f2.md
├── f3.md
├── f4.txt
├── f5.csv
├── f6.doc
├── s1
├── s2
├── s3
└── s4

4 directories, 6 files


I want to copy f1.md/f2.md/f3.md in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md.(prefix is t-)



Trying and Hope:



for file in *md;do cp -a $file t-$file;done will get result but it seems very long by using for loop. I hope if there is shorter and simple command to get the same results.







share|improve this question






















  • a bit shorter but still using loop for file in *md;do cp -a ,t-$file ;done
    – Î±Ò“sнιη
    Nov 5 '17 at 13:31










  • @αғsнιη Hi, could you explain ,t-$file is the same as t-$file?
    – Jack
    Nov 5 '17 at 13:51






  • 1




    that's just a brace expansion and expands to $file t-$file.
    – Î±Ò“sнιη
    Nov 5 '17 at 14:41














up vote
1
down vote

favorite












Example:



.
├── f1.md
├── f2.md
├── f3.md
├── f4.txt
├── f5.csv
├── f6.doc
├── s1
├── s2
├── s3
└── s4

4 directories, 6 files


I want to copy f1.md/f2.md/f3.md in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md.(prefix is t-)



Trying and Hope:



for file in *md;do cp -a $file t-$file;done will get result but it seems very long by using for loop. I hope if there is shorter and simple command to get the same results.







share|improve this question






















  • a bit shorter but still using loop for file in *md;do cp -a ,t-$file ;done
    – Î±Ò“sнιη
    Nov 5 '17 at 13:31










  • @αғsнιη Hi, could you explain ,t-$file is the same as t-$file?
    – Jack
    Nov 5 '17 at 13:51






  • 1




    that's just a brace expansion and expands to $file t-$file.
    – Î±Ò“sнιη
    Nov 5 '17 at 14:41












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Example:



.
├── f1.md
├── f2.md
├── f3.md
├── f4.txt
├── f5.csv
├── f6.doc
├── s1
├── s2
├── s3
└── s4

4 directories, 6 files


I want to copy f1.md/f2.md/f3.md in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md.(prefix is t-)



Trying and Hope:



for file in *md;do cp -a $file t-$file;done will get result but it seems very long by using for loop. I hope if there is shorter and simple command to get the same results.







share|improve this question














Example:



.
├── f1.md
├── f2.md
├── f3.md
├── f4.txt
├── f5.csv
├── f6.doc
├── s1
├── s2
├── s3
└── s4

4 directories, 6 files


I want to copy f1.md/f2.md/f3.md in current directory, and the results would be t-f1.md/t-f2.md/t-f3.md.(prefix is t-)



Trying and Hope:



for file in *md;do cp -a $file t-$file;done will get result but it seems very long by using for loop. I hope if there is shorter and simple command to get the same results.









share|improve this question













share|improve this question




share|improve this question








edited Nov 5 '17 at 13:10

























asked Nov 5 '17 at 9:33









Jack

354




354











  • a bit shorter but still using loop for file in *md;do cp -a ,t-$file ;done
    – Î±Ò“sнιη
    Nov 5 '17 at 13:31










  • @αғsнιη Hi, could you explain ,t-$file is the same as t-$file?
    – Jack
    Nov 5 '17 at 13:51






  • 1




    that's just a brace expansion and expands to $file t-$file.
    – Î±Ò“sнιη
    Nov 5 '17 at 14:41
















  • a bit shorter but still using loop for file in *md;do cp -a ,t-$file ;done
    – Î±Ò“sнιη
    Nov 5 '17 at 13:31










  • @αғsнιη Hi, could you explain ,t-$file is the same as t-$file?
    – Jack
    Nov 5 '17 at 13:51






  • 1




    that's just a brace expansion and expands to $file t-$file.
    – Î±Ò“sнιη
    Nov 5 '17 at 14:41















a bit shorter but still using loop for file in *md;do cp -a ,t-$file ;done
– Î±Ò“sнιη
Nov 5 '17 at 13:31




a bit shorter but still using loop for file in *md;do cp -a ,t-$file ;done
– Î±Ò“sнιη
Nov 5 '17 at 13:31












@αғsнιη Hi, could you explain ,t-$file is the same as t-$file?
– Jack
Nov 5 '17 at 13:51




@αғsнιη Hi, could you explain ,t-$file is the same as t-$file?
– Jack
Nov 5 '17 at 13:51




1




1




that's just a brace expansion and expands to $file t-$file.
– Î±Ò“sнιη
Nov 5 '17 at 14:41




that's just a brace expansion and expands to $file t-$file.
– Î±Ò“sнιη
Nov 5 '17 at 14:41










3 Answers
3






active

oldest

votes

















up vote
2
down vote













The shortest way with GNU parallel:



parallel --no-notice 'cp "" "t-"' ::: *.md


https://www.gnu.org/software/parallel/parallel_tutorial.html






share|improve this answer





























    up vote
    1
    down vote













    To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp rename and mv



    mkdir tmp
    cp -a *.md tmp
    (cd tmp && rename '' 't-' tmp/*.md)
    mv temp/*.md .


    This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp rename and mv.






    share|improve this answer





























      up vote
      0
      down vote













      With mmv / mcp:



      mcp '*.md' t-#1.md


      Ex. given



      $ tree .
      .
      ├── f1.md
      ├── f2.md
      ├── f3.md
      ├── f4.txt
      ├── f5.csv
      ├── f6.doc
      ├── s1
      ├── s2
      ├── s3
      └── s4

      0 directories, 10 files


      Then



      $ mcp -v '*.md' t-#1.md
      f1.md -> t-f1.md : done
      f2.md -> t-f2.md : done
      f3.md -> t-f3.md : done


      resulting in



      $ tree .
      .
      ├── f1.md
      ├── f2.md
      ├── f3.md
      ├── f4.txt
      ├── f5.csv
      ├── f6.doc
      ├── s1
      ├── s2
      ├── s3
      ├── s4
      ├── t-f1.md
      ├── t-f2.md
      └── t-f3.md

      0 directories, 13 files





      share|improve this answer




















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402616%2fhow-to-batch-cp-files-and-rename-by-adding-prefix-in-current-directory-in-one-co%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
        2
        down vote













        The shortest way with GNU parallel:



        parallel --no-notice 'cp "" "t-"' ::: *.md


        https://www.gnu.org/software/parallel/parallel_tutorial.html






        share|improve this answer


























          up vote
          2
          down vote













          The shortest way with GNU parallel:



          parallel --no-notice 'cp "" "t-"' ::: *.md


          https://www.gnu.org/software/parallel/parallel_tutorial.html






          share|improve this answer
























            up vote
            2
            down vote










            up vote
            2
            down vote









            The shortest way with GNU parallel:



            parallel --no-notice 'cp "" "t-"' ::: *.md


            https://www.gnu.org/software/parallel/parallel_tutorial.html






            share|improve this answer














            The shortest way with GNU parallel:



            parallel --no-notice 'cp "" "t-"' ::: *.md


            https://www.gnu.org/software/parallel/parallel_tutorial.html







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 5 '17 at 11:09

























            answered Nov 5 '17 at 9:58









            RomanPerekhrest

            22.5k12145




            22.5k12145






















                up vote
                1
                down vote













                To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp rename and mv



                mkdir tmp
                cp -a *.md tmp
                (cd tmp && rename '' 't-' tmp/*.md)
                mv temp/*.md .


                This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp rename and mv.






                share|improve this answer


























                  up vote
                  1
                  down vote













                  To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp rename and mv



                  mkdir tmp
                  cp -a *.md tmp
                  (cd tmp && rename '' 't-' tmp/*.md)
                  mv temp/*.md .


                  This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp rename and mv.






                  share|improve this answer
























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp rename and mv



                    mkdir tmp
                    cp -a *.md tmp
                    (cd tmp && rename '' 't-' tmp/*.md)
                    mv temp/*.md .


                    This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp rename and mv.






                    share|improve this answer














                    To copy the files the loop you already have is the shortest way to do it. If you want to do it without the loop you could combine cp rename and mv



                    mkdir tmp
                    cp -a *.md tmp
                    (cd tmp && rename '' 't-' tmp/*.md)
                    mv temp/*.md .


                    This reduces the commands you have to run (only one cp command vs one for each file), this may or may not make the program faster to run - you would need to benchmark it. But will fail if you have a very large number of files as we pass all of the files to the cp rename and mv.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 5 '17 at 10:14

























                    answered Nov 5 '17 at 9:58









                    Michael Daffin

                    2,5801517




                    2,5801517




















                        up vote
                        0
                        down vote













                        With mmv / mcp:



                        mcp '*.md' t-#1.md


                        Ex. given



                        $ tree .
                        .
                        ├── f1.md
                        ├── f2.md
                        ├── f3.md
                        ├── f4.txt
                        ├── f5.csv
                        ├── f6.doc
                        ├── s1
                        ├── s2
                        ├── s3
                        └── s4

                        0 directories, 10 files


                        Then



                        $ mcp -v '*.md' t-#1.md
                        f1.md -> t-f1.md : done
                        f2.md -> t-f2.md : done
                        f3.md -> t-f3.md : done


                        resulting in



                        $ tree .
                        .
                        ├── f1.md
                        ├── f2.md
                        ├── f3.md
                        ├── f4.txt
                        ├── f5.csv
                        ├── f6.doc
                        ├── s1
                        ├── s2
                        ├── s3
                        ├── s4
                        ├── t-f1.md
                        ├── t-f2.md
                        └── t-f3.md

                        0 directories, 13 files





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          With mmv / mcp:



                          mcp '*.md' t-#1.md


                          Ex. given



                          $ tree .
                          .
                          ├── f1.md
                          ├── f2.md
                          ├── f3.md
                          ├── f4.txt
                          ├── f5.csv
                          ├── f6.doc
                          ├── s1
                          ├── s2
                          ├── s3
                          └── s4

                          0 directories, 10 files


                          Then



                          $ mcp -v '*.md' t-#1.md
                          f1.md -> t-f1.md : done
                          f2.md -> t-f2.md : done
                          f3.md -> t-f3.md : done


                          resulting in



                          $ tree .
                          .
                          ├── f1.md
                          ├── f2.md
                          ├── f3.md
                          ├── f4.txt
                          ├── f5.csv
                          ├── f6.doc
                          ├── s1
                          ├── s2
                          ├── s3
                          ├── s4
                          ├── t-f1.md
                          ├── t-f2.md
                          └── t-f3.md

                          0 directories, 13 files





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            With mmv / mcp:



                            mcp '*.md' t-#1.md


                            Ex. given



                            $ tree .
                            .
                            ├── f1.md
                            ├── f2.md
                            ├── f3.md
                            ├── f4.txt
                            ├── f5.csv
                            ├── f6.doc
                            ├── s1
                            ├── s2
                            ├── s3
                            └── s4

                            0 directories, 10 files


                            Then



                            $ mcp -v '*.md' t-#1.md
                            f1.md -> t-f1.md : done
                            f2.md -> t-f2.md : done
                            f3.md -> t-f3.md : done


                            resulting in



                            $ tree .
                            .
                            ├── f1.md
                            ├── f2.md
                            ├── f3.md
                            ├── f4.txt
                            ├── f5.csv
                            ├── f6.doc
                            ├── s1
                            ├── s2
                            ├── s3
                            ├── s4
                            ├── t-f1.md
                            ├── t-f2.md
                            └── t-f3.md

                            0 directories, 13 files





                            share|improve this answer












                            With mmv / mcp:



                            mcp '*.md' t-#1.md


                            Ex. given



                            $ tree .
                            .
                            ├── f1.md
                            ├── f2.md
                            ├── f3.md
                            ├── f4.txt
                            ├── f5.csv
                            ├── f6.doc
                            ├── s1
                            ├── s2
                            ├── s3
                            └── s4

                            0 directories, 10 files


                            Then



                            $ mcp -v '*.md' t-#1.md
                            f1.md -> t-f1.md : done
                            f2.md -> t-f2.md : done
                            f3.md -> t-f3.md : done


                            resulting in



                            $ tree .
                            .
                            ├── f1.md
                            ├── f2.md
                            ├── f3.md
                            ├── f4.txt
                            ├── f5.csv
                            ├── f6.doc
                            ├── s1
                            ├── s2
                            ├── s3
                            ├── s4
                            ├── t-f1.md
                            ├── t-f2.md
                            └── t-f3.md

                            0 directories, 13 files






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 5 '17 at 14:03









                            steeldriver

                            32k34979




                            32k34979



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402616%2fhow-to-batch-cp-files-and-rename-by-adding-prefix-in-current-directory-in-one-co%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?