How can I move all files matching a pattern into a folder?

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








up vote
1
down vote

favorite












ls | grep 'NC022.*nii'


Shows me all the files containing NC022 and nii.



But when I try to move them using



mv NC022.*nii NC022/


It complains that



mv: cannot stat 'NC022.*nii': No such file or directory


This happens also if I try this (as seen in other answers).



mv -t NC022 'ls | grep 'NC022.*nii''


I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...



How can I move all files matching a pattern into a folder?



Example of partial ls output for first command:



NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii






share|improve this question


















  • 1




    Remember that grep uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii matches zero or more characters between NC022 and nii whereas mv NC022.*nii NC022/ will only move files matching NC022. then zero or more characters followed by nii
    – steeldriver
    Apr 24 at 22:03










  • @ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
    – hirschme
    Apr 24 at 22:08






  • 2




    @hirschme please edit your question to show some of the actual filenames (for example, partial output of ls | grep 'NC022.*nii')
    – steeldriver
    Apr 24 at 22:14














up vote
1
down vote

favorite












ls | grep 'NC022.*nii'


Shows me all the files containing NC022 and nii.



But when I try to move them using



mv NC022.*nii NC022/


It complains that



mv: cannot stat 'NC022.*nii': No such file or directory


This happens also if I try this (as seen in other answers).



mv -t NC022 'ls | grep 'NC022.*nii''


I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...



How can I move all files matching a pattern into a folder?



Example of partial ls output for first command:



NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii






share|improve this question


















  • 1




    Remember that grep uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii matches zero or more characters between NC022 and nii whereas mv NC022.*nii NC022/ will only move files matching NC022. then zero or more characters followed by nii
    – steeldriver
    Apr 24 at 22:03










  • @ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
    – hirschme
    Apr 24 at 22:08






  • 2




    @hirschme please edit your question to show some of the actual filenames (for example, partial output of ls | grep 'NC022.*nii')
    – steeldriver
    Apr 24 at 22:14












up vote
1
down vote

favorite









up vote
1
down vote

favorite











ls | grep 'NC022.*nii'


Shows me all the files containing NC022 and nii.



But when I try to move them using



mv NC022.*nii NC022/


It complains that



mv: cannot stat 'NC022.*nii': No such file or directory


This happens also if I try this (as seen in other answers).



mv -t NC022 'ls | grep 'NC022.*nii''


I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...



How can I move all files matching a pattern into a folder?



Example of partial ls output for first command:



NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii






share|improve this question














ls | grep 'NC022.*nii'


Shows me all the files containing NC022 and nii.



But when I try to move them using



mv NC022.*nii NC022/


It complains that



mv: cannot stat 'NC022.*nii': No such file or directory


This happens also if I try this (as seen in other answers).



mv -t NC022 'ls | grep 'NC022.*nii''


I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...



How can I move all files matching a pattern into a folder?



Example of partial ls output for first command:



NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii








share|improve this question













share|improve this question




share|improve this question








edited Apr 24 at 22:36

























asked Apr 24 at 21:54









hirschme

296




296







  • 1




    Remember that grep uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii matches zero or more characters between NC022 and nii whereas mv NC022.*nii NC022/ will only move files matching NC022. then zero or more characters followed by nii
    – steeldriver
    Apr 24 at 22:03










  • @ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
    – hirschme
    Apr 24 at 22:08






  • 2




    @hirschme please edit your question to show some of the actual filenames (for example, partial output of ls | grep 'NC022.*nii')
    – steeldriver
    Apr 24 at 22:14












  • 1




    Remember that grep uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii matches zero or more characters between NC022 and nii whereas mv NC022.*nii NC022/ will only move files matching NC022. then zero or more characters followed by nii
    – steeldriver
    Apr 24 at 22:03










  • @ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
    – hirschme
    Apr 24 at 22:08






  • 2




    @hirschme please edit your question to show some of the actual filenames (for example, partial output of ls | grep 'NC022.*nii')
    – steeldriver
    Apr 24 at 22:14







1




1




Remember that grep uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii matches zero or more characters between NC022 and nii whereas mv NC022.*nii NC022/ will only move files matching NC022. then zero or more characters followed by nii
– steeldriver
Apr 24 at 22:03




Remember that grep uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii matches zero or more characters between NC022 and nii whereas mv NC022.*nii NC022/ will only move files matching NC022. then zero or more characters followed by nii
– steeldriver
Apr 24 at 22:03












@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
– hirschme
Apr 24 at 22:08




@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
– hirschme
Apr 24 at 22:08




2




2




@hirschme please edit your question to show some of the actual filenames (for example, partial output of ls | grep 'NC022.*nii')
– steeldriver
Apr 24 at 22:14




@hirschme please edit your question to show some of the actual filenames (for example, partial output of ls | grep 'NC022.*nii')
– steeldriver
Apr 24 at 22:14










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










You are confusing regular expression syntax (as used by grep) with glob patterns (as used by the shell).



In regex, . means any single character, and * means zero or more repetitions. So grep 'NC022.*nii' matches NC022 to nii with anything (including nothing) in between.



In contrast, . is literal in shell globs, while * itself means zero or more characters. So NC022.*nii matches NC022. to nii with anything (including nothing) in between.



In particular, if you are trying to match all files with a .nii extension, the . is in the wrong place: you'd want NC022*.nii i.e.



mv NC022*.nii NC022/





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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1027931%2fhow-can-i-move-all-files-matching-a-pattern-into-a-folder%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
    4
    down vote



    accepted










    You are confusing regular expression syntax (as used by grep) with glob patterns (as used by the shell).



    In regex, . means any single character, and * means zero or more repetitions. So grep 'NC022.*nii' matches NC022 to nii with anything (including nothing) in between.



    In contrast, . is literal in shell globs, while * itself means zero or more characters. So NC022.*nii matches NC022. to nii with anything (including nothing) in between.



    In particular, if you are trying to match all files with a .nii extension, the . is in the wrong place: you'd want NC022*.nii i.e.



    mv NC022*.nii NC022/





    share|improve this answer
























      up vote
      4
      down vote



      accepted










      You are confusing regular expression syntax (as used by grep) with glob patterns (as used by the shell).



      In regex, . means any single character, and * means zero or more repetitions. So grep 'NC022.*nii' matches NC022 to nii with anything (including nothing) in between.



      In contrast, . is literal in shell globs, while * itself means zero or more characters. So NC022.*nii matches NC022. to nii with anything (including nothing) in between.



      In particular, if you are trying to match all files with a .nii extension, the . is in the wrong place: you'd want NC022*.nii i.e.



      mv NC022*.nii NC022/





      share|improve this answer






















        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        You are confusing regular expression syntax (as used by grep) with glob patterns (as used by the shell).



        In regex, . means any single character, and * means zero or more repetitions. So grep 'NC022.*nii' matches NC022 to nii with anything (including nothing) in between.



        In contrast, . is literal in shell globs, while * itself means zero or more characters. So NC022.*nii matches NC022. to nii with anything (including nothing) in between.



        In particular, if you are trying to match all files with a .nii extension, the . is in the wrong place: you'd want NC022*.nii i.e.



        mv NC022*.nii NC022/





        share|improve this answer












        You are confusing regular expression syntax (as used by grep) with glob patterns (as used by the shell).



        In regex, . means any single character, and * means zero or more repetitions. So grep 'NC022.*nii' matches NC022 to nii with anything (including nothing) in between.



        In contrast, . is literal in shell globs, while * itself means zero or more characters. So NC022.*nii matches NC022. to nii with anything (including nothing) in between.



        In particular, if you are trying to match all files with a .nii extension, the . is in the wrong place: you'd want NC022*.nii i.e.



        mv NC022*.nii NC022/






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 24 at 23:55









        steeldriver

        62.8k1196165




        62.8k1196165



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1027931%2fhow-can-i-move-all-files-matching-a-pattern-into-a-folder%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Which professions warranted travel in Medieval times?

            How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

            Trouble downloading packages list due to a “Hash sum mismatch” error