The best way to expand glob pattern?

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












27















I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?










share|improve this question



















  • 2





    You don't need to do anything special, just don't quote the *.

    – Kevin
    Mar 12 '12 at 14:32






  • 1





    Though if you're going to be trying to parse it, use an array like the answer says.

    – Kevin
    Mar 12 '12 at 14:34















27















I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?










share|improve this question



















  • 2





    You don't need to do anything special, just don't quote the *.

    – Kevin
    Mar 12 '12 at 14:32






  • 1





    Though if you're going to be trying to parse it, use an array like the answer says.

    – Kevin
    Mar 12 '12 at 14:34













27












27








27


3






I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?










share|improve this question
















I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?







bash wildcards






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 14 '13 at 6:45









vadipp

1838




1838










asked Mar 12 '12 at 13:28









RogachRogach

1,986102638




1,986102638







  • 2





    You don't need to do anything special, just don't quote the *.

    – Kevin
    Mar 12 '12 at 14:32






  • 1





    Though if you're going to be trying to parse it, use an array like the answer says.

    – Kevin
    Mar 12 '12 at 14:34












  • 2





    You don't need to do anything special, just don't quote the *.

    – Kevin
    Mar 12 '12 at 14:32






  • 1





    Though if you're going to be trying to parse it, use an array like the answer says.

    – Kevin
    Mar 12 '12 at 14:34







2




2





You don't need to do anything special, just don't quote the *.

– Kevin
Mar 12 '12 at 14:32





You don't need to do anything special, just don't quote the *.

– Kevin
Mar 12 '12 at 14:32




1




1





Though if you're going to be trying to parse it, use an array like the answer says.

– Kevin
Mar 12 '12 at 14:34





Though if you're going to be trying to parse it, use an array like the answer says.

– Kevin
Mar 12 '12 at 14:34










2 Answers
2






active

oldest

votes


















35














Just let it expand inside an array declaration's right side:



list=(../smth*/) # grab the list
echo "$#list[@]" # print array length
echo "$list[@]" # print array elements
for file in "$list[@]"; do echo "$file"; done # loop over the array




Note that the shell option nullglob needs to be set.
It is not set by default.

It prevents an error in case the glob (or one of multiple globs) does not match any name.



Set it in bash with



shopt -s nullglob


or in zsh with



setopt nullglob





share|improve this answer

























  • And how do I print that list afterwards?

    – Rogach
    Mar 12 '12 at 13:38











  • It is just a regular array. You can do whatever you can with any array. Added some examples.

    – manatwork
    Mar 12 '12 at 13:44






  • 1





    There is a problem. If pattern matches no files, it prints itself - which is not very good.

    – Rogach
    Mar 12 '12 at 14:21






  • 5





    Ah, shopt -s nullglob solves it.

    – Rogach
    Mar 12 '12 at 14:25






  • 1





    @lindhe, unsetopt and the same parameter as used for setopt.

    – manatwork
    Dec 5 '15 at 18:48


















0














No need to overcomplicate things:



echo your/stuff*





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%2f34011%2fthe-best-way-to-expand-glob-pattern%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









    35














    Just let it expand inside an array declaration's right side:



    list=(../smth*/) # grab the list
    echo "$#list[@]" # print array length
    echo "$list[@]" # print array elements
    for file in "$list[@]"; do echo "$file"; done # loop over the array




    Note that the shell option nullglob needs to be set.
    It is not set by default.

    It prevents an error in case the glob (or one of multiple globs) does not match any name.



    Set it in bash with



    shopt -s nullglob


    or in zsh with



    setopt nullglob





    share|improve this answer

























    • And how do I print that list afterwards?

      – Rogach
      Mar 12 '12 at 13:38











    • It is just a regular array. You can do whatever you can with any array. Added some examples.

      – manatwork
      Mar 12 '12 at 13:44






    • 1





      There is a problem. If pattern matches no files, it prints itself - which is not very good.

      – Rogach
      Mar 12 '12 at 14:21






    • 5





      Ah, shopt -s nullglob solves it.

      – Rogach
      Mar 12 '12 at 14:25






    • 1





      @lindhe, unsetopt and the same parameter as used for setopt.

      – manatwork
      Dec 5 '15 at 18:48















    35














    Just let it expand inside an array declaration's right side:



    list=(../smth*/) # grab the list
    echo "$#list[@]" # print array length
    echo "$list[@]" # print array elements
    for file in "$list[@]"; do echo "$file"; done # loop over the array




    Note that the shell option nullglob needs to be set.
    It is not set by default.

    It prevents an error in case the glob (or one of multiple globs) does not match any name.



    Set it in bash with



    shopt -s nullglob


    or in zsh with



    setopt nullglob





    share|improve this answer

























    • And how do I print that list afterwards?

      – Rogach
      Mar 12 '12 at 13:38











    • It is just a regular array. You can do whatever you can with any array. Added some examples.

      – manatwork
      Mar 12 '12 at 13:44






    • 1





      There is a problem. If pattern matches no files, it prints itself - which is not very good.

      – Rogach
      Mar 12 '12 at 14:21






    • 5





      Ah, shopt -s nullglob solves it.

      – Rogach
      Mar 12 '12 at 14:25






    • 1





      @lindhe, unsetopt and the same parameter as used for setopt.

      – manatwork
      Dec 5 '15 at 18:48













    35












    35








    35







    Just let it expand inside an array declaration's right side:



    list=(../smth*/) # grab the list
    echo "$#list[@]" # print array length
    echo "$list[@]" # print array elements
    for file in "$list[@]"; do echo "$file"; done # loop over the array




    Note that the shell option nullglob needs to be set.
    It is not set by default.

    It prevents an error in case the glob (or one of multiple globs) does not match any name.



    Set it in bash with



    shopt -s nullglob


    or in zsh with



    setopt nullglob





    share|improve this answer















    Just let it expand inside an array declaration's right side:



    list=(../smth*/) # grab the list
    echo "$#list[@]" # print array length
    echo "$list[@]" # print array elements
    for file in "$list[@]"; do echo "$file"; done # loop over the array




    Note that the shell option nullglob needs to be set.
    It is not set by default.

    It prevents an error in case the glob (or one of multiple globs) does not match any name.



    Set it in bash with



    shopt -s nullglob


    or in zsh with



    setopt nullglob






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 2 '16 at 15:59









    Jakuje

    16.5k53155




    16.5k53155










    answered Mar 12 '12 at 13:37









    manatworkmanatwork

    22k38385




    22k38385












    • And how do I print that list afterwards?

      – Rogach
      Mar 12 '12 at 13:38











    • It is just a regular array. You can do whatever you can with any array. Added some examples.

      – manatwork
      Mar 12 '12 at 13:44






    • 1





      There is a problem. If pattern matches no files, it prints itself - which is not very good.

      – Rogach
      Mar 12 '12 at 14:21






    • 5





      Ah, shopt -s nullglob solves it.

      – Rogach
      Mar 12 '12 at 14:25






    • 1





      @lindhe, unsetopt and the same parameter as used for setopt.

      – manatwork
      Dec 5 '15 at 18:48

















    • And how do I print that list afterwards?

      – Rogach
      Mar 12 '12 at 13:38











    • It is just a regular array. You can do whatever you can with any array. Added some examples.

      – manatwork
      Mar 12 '12 at 13:44






    • 1





      There is a problem. If pattern matches no files, it prints itself - which is not very good.

      – Rogach
      Mar 12 '12 at 14:21






    • 5





      Ah, shopt -s nullglob solves it.

      – Rogach
      Mar 12 '12 at 14:25






    • 1





      @lindhe, unsetopt and the same parameter as used for setopt.

      – manatwork
      Dec 5 '15 at 18:48
















    And how do I print that list afterwards?

    – Rogach
    Mar 12 '12 at 13:38





    And how do I print that list afterwards?

    – Rogach
    Mar 12 '12 at 13:38













    It is just a regular array. You can do whatever you can with any array. Added some examples.

    – manatwork
    Mar 12 '12 at 13:44





    It is just a regular array. You can do whatever you can with any array. Added some examples.

    – manatwork
    Mar 12 '12 at 13:44




    1




    1





    There is a problem. If pattern matches no files, it prints itself - which is not very good.

    – Rogach
    Mar 12 '12 at 14:21





    There is a problem. If pattern matches no files, it prints itself - which is not very good.

    – Rogach
    Mar 12 '12 at 14:21




    5




    5





    Ah, shopt -s nullglob solves it.

    – Rogach
    Mar 12 '12 at 14:25





    Ah, shopt -s nullglob solves it.

    – Rogach
    Mar 12 '12 at 14:25




    1




    1





    @lindhe, unsetopt and the same parameter as used for setopt.

    – manatwork
    Dec 5 '15 at 18:48





    @lindhe, unsetopt and the same parameter as used for setopt.

    – manatwork
    Dec 5 '15 at 18:48













    0














    No need to overcomplicate things:



    echo your/stuff*





    share|improve this answer



























      0














      No need to overcomplicate things:



      echo your/stuff*





      share|improve this answer

























        0












        0








        0







        No need to overcomplicate things:



        echo your/stuff*





        share|improve this answer













        No need to overcomplicate things:



        echo your/stuff*






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 8 at 17:28









        Alexei AverchenkoAlexei Averchenko

        1011




        1011



























            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%2f34011%2fthe-best-way-to-expand-glob-pattern%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

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)