How to match a file name by specifying a prefix substring?

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











up vote
1
down vote

favorite












I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.



I have tried to select only the files whose names start with either massif or calgrind using the following command:



scp my-machine:/home/user/a-folder/[massif,callgrind]* .


However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.



What do I have to change to make it work correctly?










share|improve this question









New contributor




nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
    – nico
    20 hours ago











  • The brace expansion may produce filenames that do not exist.
    – Fólkvangr
    6 hours ago














up vote
1
down vote

favorite












I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.



I have tried to select only the files whose names start with either massif or calgrind using the following command:



scp my-machine:/home/user/a-folder/[massif,callgrind]* .


However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.



What do I have to change to make it work correctly?










share|improve this question









New contributor




nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
    – nico
    20 hours ago











  • The brace expansion may produce filenames that do not exist.
    – Fólkvangr
    6 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.



I have tried to select only the files whose names start with either massif or calgrind using the following command:



scp my-machine:/home/user/a-folder/[massif,callgrind]* .


However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.



What do I have to change to make it work correctly?










share|improve this question









New contributor




nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.



I have tried to select only the files whose names start with either massif or calgrind using the following command:



scp my-machine:/home/user/a-folder/[massif,callgrind]* .


However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.



What do I have to change to make it work correctly?







bash shell-script filenames wildcards bash-expansion






share|improve this question









New contributor




nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 7 mins ago









Fólkvangr

38010




38010






New contributor




nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 20 hours ago









nico

83




83




New contributor




nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






nico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
    – nico
    20 hours ago











  • The brace expansion may produce filenames that do not exist.
    – Fólkvangr
    6 hours ago
















  • It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
    – nico
    20 hours ago











  • The brace expansion may produce filenames that do not exist.
    – Fólkvangr
    6 hours ago















It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
– nico
20 hours ago





It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
– nico
20 hours ago













The brace expansion may produce filenames that do not exist.
– Fólkvangr
6 hours ago




The brace expansion may produce filenames that do not exist.
– Fólkvangr
6 hours ago










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










It sounds like you want curly-brackets there:



scp my-machine:/home/user/a-folder/massif,callgrind* .


Otherwise you're matching on single characters, rather than the two complete strings.



bash does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION section. Here, the [ type is covered under "Pathname Expansion" (it matches on pathnames) and { under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).






share|improve this answer










New contributor




Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
    – nico
    20 hours ago










  • Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
    – nico
    20 hours ago










  • I've just added an edit with some references to documentation, hope that helps!
    – Jeremy Kerr
    20 hours ago

















up vote
0
down vote













Bash provides some extended patterns if the extglob shell option is enabled (c.f. Bash Reference Manual).



prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .


The first command enables the extglob shell option on the remote host. The second command allows to copy filenames that begin with foo or bar in the current directory of the local machine.



Note: I recommend editing the configuration file .bash_profile manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.






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
    );



    );






    nico is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477664%2fhow-to-match-a-file-name-by-specifying-a-prefix-substring%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    It sounds like you want curly-brackets there:



    scp my-machine:/home/user/a-folder/massif,callgrind* .


    Otherwise you're matching on single characters, rather than the two complete strings.



    bash does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION section. Here, the [ type is covered under "Pathname Expansion" (it matches on pathnames) and { under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).






    share|improve this answer










    New contributor




    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















    • Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
      – nico
      20 hours ago










    • Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
      – nico
      20 hours ago










    • I've just added an edit with some references to documentation, hope that helps!
      – Jeremy Kerr
      20 hours ago














    up vote
    1
    down vote



    accepted










    It sounds like you want curly-brackets there:



    scp my-machine:/home/user/a-folder/massif,callgrind* .


    Otherwise you're matching on single characters, rather than the two complete strings.



    bash does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION section. Here, the [ type is covered under "Pathname Expansion" (it matches on pathnames) and { under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).






    share|improve this answer










    New contributor




    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















    • Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
      – nico
      20 hours ago










    • Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
      – nico
      20 hours ago










    • I've just added an edit with some references to documentation, hope that helps!
      – Jeremy Kerr
      20 hours ago












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    It sounds like you want curly-brackets there:



    scp my-machine:/home/user/a-folder/massif,callgrind* .


    Otherwise you're matching on single characters, rather than the two complete strings.



    bash does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION section. Here, the [ type is covered under "Pathname Expansion" (it matches on pathnames) and { under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).






    share|improve this answer










    New contributor




    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    It sounds like you want curly-brackets there:



    scp my-machine:/home/user/a-folder/massif,callgrind* .


    Otherwise you're matching on single characters, rather than the two complete strings.



    bash does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION section. Here, the [ type is covered under "Pathname Expansion" (it matches on pathnames) and { under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).







    share|improve this answer










    New contributor




    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer








    edited 20 hours ago





















    New contributor




    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered 20 hours ago









    Jeremy Kerr

    1305




    1305




    New contributor




    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    Jeremy Kerr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    • Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
      – nico
      20 hours ago










    • Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
      – nico
      20 hours ago










    • I've just added an edit with some references to documentation, hope that helps!
      – Jeremy Kerr
      20 hours ago
















    • Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
      – nico
      20 hours ago










    • Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
      – nico
      20 hours ago










    • I've just added an edit with some references to documentation, hope that helps!
      – Jeremy Kerr
      20 hours ago















    Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
    – nico
    20 hours ago




    Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
    – nico
    20 hours ago












    Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
    – nico
    20 hours ago




    Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
    – nico
    20 hours ago












    I've just added an edit with some references to documentation, hope that helps!
    – Jeremy Kerr
    20 hours ago




    I've just added an edit with some references to documentation, hope that helps!
    – Jeremy Kerr
    20 hours ago












    up vote
    0
    down vote













    Bash provides some extended patterns if the extglob shell option is enabled (c.f. Bash Reference Manual).



    prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
    prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .


    The first command enables the extglob shell option on the remote host. The second command allows to copy filenames that begin with foo or bar in the current directory of the local machine.



    Note: I recommend editing the configuration file .bash_profile manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.






    share|improve this answer


























      up vote
      0
      down vote













      Bash provides some extended patterns if the extglob shell option is enabled (c.f. Bash Reference Manual).



      prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
      prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .


      The first command enables the extglob shell option on the remote host. The second command allows to copy filenames that begin with foo or bar in the current directory of the local machine.



      Note: I recommend editing the configuration file .bash_profile manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Bash provides some extended patterns if the extglob shell option is enabled (c.f. Bash Reference Manual).



        prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
        prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .


        The first command enables the extglob shell option on the remote host. The second command allows to copy filenames that begin with foo or bar in the current directory of the local machine.



        Note: I recommend editing the configuration file .bash_profile manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.






        share|improve this answer














        Bash provides some extended patterns if the extglob shell option is enabled (c.f. Bash Reference Manual).



        prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
        prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .


        The first command enables the extglob shell option on the remote host. The second command allows to copy filenames that begin with foo or bar in the current directory of the local machine.



        Note: I recommend editing the configuration file .bash_profile manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 11 hours ago

























        answered 15 hours ago









        Fólkvangr

        38010




        38010




















            nico is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            nico is a new contributor. Be nice, and check out our Code of Conduct.












            nico is a new contributor. Be nice, and check out our Code of Conduct.











            nico is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477664%2fhow-to-match-a-file-name-by-specifying-a-prefix-substring%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?