'regex' and 'name' directives in findFind and regexUnderstanding -regex with GNU findFind and regexrsync: how to include files with name ending in a specific stringRecursive grep vs find / -type f -exec grep ; Which is more efficient/faster?Cannot prompt user using rm bultin prompt option -i with xargs and findfind(1): how is the star wildcard implemented for it to fail on some filenames?Explanation of % directives in find -printfUsing regex quantifier with findfind and regex - find files with names starting with given nameregex with find commandFind with Regex, and specific Path

Why must Chinese maps be obfuscated?

Alignment of various blocks in tikz

As an international instructor, should I openly talk about my accent?

Elements other than carbon that can form many different compounds by bonding to themselves?

How did Captain America manage to do this?

How does Captain America channel this power?

Was there a shared-world project before "Thieves World"?

What makes accurate emulation of old systems a difficult task?

Why does nature favour the Laplacian?

Pre-plastic human skin alternative

Was there a Viking Exchange as well as a Columbian one?

Does a large simulator bay have standard public address announcements?

Don’t seats that recline flat defeat the purpose of having seatbelts?

Phrase for the opposite of "foolproof"

Is the claim "Employers won't employ people with no 'social media presence'" realistic?

What does the integral of a function times a function of a random variable represent, conceptually?

can anyone help me with this awful query plan?

Implications of cigar-shaped bodies having rings?

Is there really no use for MD5 anymore?

What is the philosophical significance of speech acts/implicature?

What's the polite way to say "I need to urinate"?

How can I practically buy stocks?

Betweenness centrality formula

Why did C use the -> operator instead of reusing the . operator?



'regex' and 'name' directives in find


Find and regexUnderstanding -regex with GNU findFind and regexrsync: how to include files with name ending in a specific stringRecursive grep vs find / -type f -exec grep ; Which is more efficient/faster?Cannot prompt user using rm bultin prompt option -i with xargs and findfind(1): how is the star wildcard implemented for it to fail on some filenames?Explanation of % directives in find -printfUsing regex quantifier with findfind and regex - find files with names starting with given nameregex with find commandFind with Regex, and specific Path






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















What sort of regex syntax does -name option of find command expect? I was of the impression that it would be the same as for -regex but that doesn't seem to be the case.



$ mkdir test && cd test
$ touch .sw.
$ touch .swp
$ touch .abc.swo
$ touch notswap.py
$ find . -name "*sw." -type f
./.sw.
$ find . -name "*sw*" -type f
./notswap.py
./.sw.
./.swp
./.abc.swo
$ find . -regex ".*sw." -type f
./.sw.
./.swp
./.abc.swo


FWIW, I am aware that -regex matches the entire path and -name only the base of the filename. I hope it's clear that that's not the issue here. As a more concrete question, how do I match all files ending with .swx where x can be any character using the -name option.



Meta info:



$ find --version
find (GNU findutils) 4.5.11
...
$ echo $0
bash









share|improve this question









New contributor




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




















  • Linking in: unix.stackexchange.com/q/43913/117549 and unix.stackexchange.com/q/105688/117549

    – Jeff Schaller
    11 hours ago

















3















What sort of regex syntax does -name option of find command expect? I was of the impression that it would be the same as for -regex but that doesn't seem to be the case.



$ mkdir test && cd test
$ touch .sw.
$ touch .swp
$ touch .abc.swo
$ touch notswap.py
$ find . -name "*sw." -type f
./.sw.
$ find . -name "*sw*" -type f
./notswap.py
./.sw.
./.swp
./.abc.swo
$ find . -regex ".*sw." -type f
./.sw.
./.swp
./.abc.swo


FWIW, I am aware that -regex matches the entire path and -name only the base of the filename. I hope it's clear that that's not the issue here. As a more concrete question, how do I match all files ending with .swx where x can be any character using the -name option.



Meta info:



$ find --version
find (GNU findutils) 4.5.11
...
$ echo $0
bash









share|improve this question









New contributor




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




















  • Linking in: unix.stackexchange.com/q/43913/117549 and unix.stackexchange.com/q/105688/117549

    – Jeff Schaller
    11 hours ago













3












3








3








What sort of regex syntax does -name option of find command expect? I was of the impression that it would be the same as for -regex but that doesn't seem to be the case.



$ mkdir test && cd test
$ touch .sw.
$ touch .swp
$ touch .abc.swo
$ touch notswap.py
$ find . -name "*sw." -type f
./.sw.
$ find . -name "*sw*" -type f
./notswap.py
./.sw.
./.swp
./.abc.swo
$ find . -regex ".*sw." -type f
./.sw.
./.swp
./.abc.swo


FWIW, I am aware that -regex matches the entire path and -name only the base of the filename. I hope it's clear that that's not the issue here. As a more concrete question, how do I match all files ending with .swx where x can be any character using the -name option.



Meta info:



$ find --version
find (GNU findutils) 4.5.11
...
$ echo $0
bash









share|improve this question









New contributor




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












What sort of regex syntax does -name option of find command expect? I was of the impression that it would be the same as for -regex but that doesn't seem to be the case.



$ mkdir test && cd test
$ touch .sw.
$ touch .swp
$ touch .abc.swo
$ touch notswap.py
$ find . -name "*sw." -type f
./.sw.
$ find . -name "*sw*" -type f
./notswap.py
./.sw.
./.swp
./.abc.swo
$ find . -regex ".*sw." -type f
./.sw.
./.swp
./.abc.swo


FWIW, I am aware that -regex matches the entire path and -name only the base of the filename. I hope it's clear that that's not the issue here. As a more concrete question, how do I match all files ending with .swx where x can be any character using the -name option.



Meta info:



$ find --version
find (GNU findutils) 4.5.11
...
$ echo $0
bash






find regular-expression






share|improve this question









New contributor




stillanoob 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




stillanoob 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 11 hours ago









Jeff Schaller

45.4k1164147




45.4k1164147






New contributor




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









asked 12 hours ago









stillanoobstillanoob

1163




1163




New contributor




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





New contributor





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






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












  • Linking in: unix.stackexchange.com/q/43913/117549 and unix.stackexchange.com/q/105688/117549

    – Jeff Schaller
    11 hours ago

















  • Linking in: unix.stackexchange.com/q/43913/117549 and unix.stackexchange.com/q/105688/117549

    – Jeff Schaller
    11 hours ago
















Linking in: unix.stackexchange.com/q/43913/117549 and unix.stackexchange.com/q/105688/117549

– Jeff Schaller
11 hours ago





Linking in: unix.stackexchange.com/q/43913/117549 and unix.stackexchange.com/q/105688/117549

– Jeff Schaller
11 hours ago










1 Answer
1






active

oldest

votes


















7














-name pattern
Base of file name (the path with the leading directories
removed) matches shell pattern pattern. Because the leading
directories are removed, the file names considered for a match
with -name will never include a slash, so `-name a/b' will
never match anything (you probably need to use -path instead).
A warning is issued if you try to do this, unless the
environment variable POSIXLY_CORRECT is set. The
metacharacters (`*', `?', and `[]') match a `.' at the start
of the base name (this is a change in findutils-4.2.2; see
section STANDARDS CONFORMANCE below). To ignore a directory
and the files under it, use -prune; see an example in the
description of -path. Braces are not recognised as being
special, despite the fact that some shells including Bash
imbue braces with a special meaning in shell patterns. The
filename matching is performed with the use of the fnmatch(3)
library function. Don't forget to enclose the pattern in
quotes in order to protect it from expansion by the shell.


It uses shell patterns not regex.



Source: find(1)




From the GNU manual under name:




Here are ways to search for files whose name matches a certain pattern. See Shell Pattern Matching, for a description of the pattern arguments to these tests.




2.1.4 Shell Pattern Matching



find and locate can compare file names, or parts of file names, to shell patterns. A shell pattern is a string that may contain the following special characters, which are known as wildcards or metacharacters.



You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.




  • *

    Matches any zero or more characters.


  • ?

    Matches any one character.


  • [string]

    Matches exactly one character that is a member of the string string. This is called a character class. As a shorthand, string may contain ranges, which consist of two characters with a dash between them. For example, the class ‘[a-z0-9_]’ matches a lowercase letter, a number, or an underscore. You can negate a class by placing a ‘!’ or ‘^’ immediately after the opening bracket. Thus, ‘[^A-Z@]’ matches any character except an uppercase letter or an at sign.




  • Removes the special meaning of the character that follows it. This works even in character classes.

    In the find tests that do shell pattern matching (‘-name’, ‘-wholename’, etc.), wildcards in the pattern will match a ‘.’ at the beginning of a file name. This is also the case for locate. Thus, ‘find -name '*macs'’ will match a file named .emacs, as will ‘locate '*macs'’.

Slash characters have no special significance in the shell pattern matching that find and locate do, unlike in the shell, in which wildcards do not match them. Therefore, a pattern ‘foobar’ can match a file name ‘foo3/bar’, and a pattern ‘./srsc’ can match a file name ‘./src/misc’.



If you want to locate some files with the ‘locate’ command but don't need to see the full list you can use the ‘--limit’ option to see just a small number of results, or the ‘--count’ option to display only the total number of matches.




To answer your question:



find . -name "*.sw?" -type f





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



    );






    stillanoob 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%2f515698%2fregex-and-name-directives-in-find%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    -name pattern
    Base of file name (the path with the leading directories
    removed) matches shell pattern pattern. Because the leading
    directories are removed, the file names considered for a match
    with -name will never include a slash, so `-name a/b' will
    never match anything (you probably need to use -path instead).
    A warning is issued if you try to do this, unless the
    environment variable POSIXLY_CORRECT is set. The
    metacharacters (`*', `?', and `[]') match a `.' at the start
    of the base name (this is a change in findutils-4.2.2; see
    section STANDARDS CONFORMANCE below). To ignore a directory
    and the files under it, use -prune; see an example in the
    description of -path. Braces are not recognised as being
    special, despite the fact that some shells including Bash
    imbue braces with a special meaning in shell patterns. The
    filename matching is performed with the use of the fnmatch(3)
    library function. Don't forget to enclose the pattern in
    quotes in order to protect it from expansion by the shell.


    It uses shell patterns not regex.



    Source: find(1)




    From the GNU manual under name:




    Here are ways to search for files whose name matches a certain pattern. See Shell Pattern Matching, for a description of the pattern arguments to these tests.




    2.1.4 Shell Pattern Matching



    find and locate can compare file names, or parts of file names, to shell patterns. A shell pattern is a string that may contain the following special characters, which are known as wildcards or metacharacters.



    You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.




    • *

      Matches any zero or more characters.


    • ?

      Matches any one character.


    • [string]

      Matches exactly one character that is a member of the string string. This is called a character class. As a shorthand, string may contain ranges, which consist of two characters with a dash between them. For example, the class ‘[a-z0-9_]’ matches a lowercase letter, a number, or an underscore. You can negate a class by placing a ‘!’ or ‘^’ immediately after the opening bracket. Thus, ‘[^A-Z@]’ matches any character except an uppercase letter or an at sign.




    • Removes the special meaning of the character that follows it. This works even in character classes.

      In the find tests that do shell pattern matching (‘-name’, ‘-wholename’, etc.), wildcards in the pattern will match a ‘.’ at the beginning of a file name. This is also the case for locate. Thus, ‘find -name '*macs'’ will match a file named .emacs, as will ‘locate '*macs'’.

    Slash characters have no special significance in the shell pattern matching that find and locate do, unlike in the shell, in which wildcards do not match them. Therefore, a pattern ‘foobar’ can match a file name ‘foo3/bar’, and a pattern ‘./srsc’ can match a file name ‘./src/misc’.



    If you want to locate some files with the ‘locate’ command but don't need to see the full list you can use the ‘--limit’ option to see just a small number of results, or the ‘--count’ option to display only the total number of matches.




    To answer your question:



    find . -name "*.sw?" -type f





    share|improve this answer





























      7














      -name pattern
      Base of file name (the path with the leading directories
      removed) matches shell pattern pattern. Because the leading
      directories are removed, the file names considered for a match
      with -name will never include a slash, so `-name a/b' will
      never match anything (you probably need to use -path instead).
      A warning is issued if you try to do this, unless the
      environment variable POSIXLY_CORRECT is set. The
      metacharacters (`*', `?', and `[]') match a `.' at the start
      of the base name (this is a change in findutils-4.2.2; see
      section STANDARDS CONFORMANCE below). To ignore a directory
      and the files under it, use -prune; see an example in the
      description of -path. Braces are not recognised as being
      special, despite the fact that some shells including Bash
      imbue braces with a special meaning in shell patterns. The
      filename matching is performed with the use of the fnmatch(3)
      library function. Don't forget to enclose the pattern in
      quotes in order to protect it from expansion by the shell.


      It uses shell patterns not regex.



      Source: find(1)




      From the GNU manual under name:




      Here are ways to search for files whose name matches a certain pattern. See Shell Pattern Matching, for a description of the pattern arguments to these tests.




      2.1.4 Shell Pattern Matching



      find and locate can compare file names, or parts of file names, to shell patterns. A shell pattern is a string that may contain the following special characters, which are known as wildcards or metacharacters.



      You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.




      • *

        Matches any zero or more characters.


      • ?

        Matches any one character.


      • [string]

        Matches exactly one character that is a member of the string string. This is called a character class. As a shorthand, string may contain ranges, which consist of two characters with a dash between them. For example, the class ‘[a-z0-9_]’ matches a lowercase letter, a number, or an underscore. You can negate a class by placing a ‘!’ or ‘^’ immediately after the opening bracket. Thus, ‘[^A-Z@]’ matches any character except an uppercase letter or an at sign.




      • Removes the special meaning of the character that follows it. This works even in character classes.

        In the find tests that do shell pattern matching (‘-name’, ‘-wholename’, etc.), wildcards in the pattern will match a ‘.’ at the beginning of a file name. This is also the case for locate. Thus, ‘find -name '*macs'’ will match a file named .emacs, as will ‘locate '*macs'’.

      Slash characters have no special significance in the shell pattern matching that find and locate do, unlike in the shell, in which wildcards do not match them. Therefore, a pattern ‘foobar’ can match a file name ‘foo3/bar’, and a pattern ‘./srsc’ can match a file name ‘./src/misc’.



      If you want to locate some files with the ‘locate’ command but don't need to see the full list you can use the ‘--limit’ option to see just a small number of results, or the ‘--count’ option to display only the total number of matches.




      To answer your question:



      find . -name "*.sw?" -type f





      share|improve this answer



























        7












        7








        7







        -name pattern
        Base of file name (the path with the leading directories
        removed) matches shell pattern pattern. Because the leading
        directories are removed, the file names considered for a match
        with -name will never include a slash, so `-name a/b' will
        never match anything (you probably need to use -path instead).
        A warning is issued if you try to do this, unless the
        environment variable POSIXLY_CORRECT is set. The
        metacharacters (`*', `?', and `[]') match a `.' at the start
        of the base name (this is a change in findutils-4.2.2; see
        section STANDARDS CONFORMANCE below). To ignore a directory
        and the files under it, use -prune; see an example in the
        description of -path. Braces are not recognised as being
        special, despite the fact that some shells including Bash
        imbue braces with a special meaning in shell patterns. The
        filename matching is performed with the use of the fnmatch(3)
        library function. Don't forget to enclose the pattern in
        quotes in order to protect it from expansion by the shell.


        It uses shell patterns not regex.



        Source: find(1)




        From the GNU manual under name:




        Here are ways to search for files whose name matches a certain pattern. See Shell Pattern Matching, for a description of the pattern arguments to these tests.




        2.1.4 Shell Pattern Matching



        find and locate can compare file names, or parts of file names, to shell patterns. A shell pattern is a string that may contain the following special characters, which are known as wildcards or metacharacters.



        You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.




        • *

          Matches any zero or more characters.


        • ?

          Matches any one character.


        • [string]

          Matches exactly one character that is a member of the string string. This is called a character class. As a shorthand, string may contain ranges, which consist of two characters with a dash between them. For example, the class ‘[a-z0-9_]’ matches a lowercase letter, a number, or an underscore. You can negate a class by placing a ‘!’ or ‘^’ immediately after the opening bracket. Thus, ‘[^A-Z@]’ matches any character except an uppercase letter or an at sign.




        • Removes the special meaning of the character that follows it. This works even in character classes.

          In the find tests that do shell pattern matching (‘-name’, ‘-wholename’, etc.), wildcards in the pattern will match a ‘.’ at the beginning of a file name. This is also the case for locate. Thus, ‘find -name '*macs'’ will match a file named .emacs, as will ‘locate '*macs'’.

        Slash characters have no special significance in the shell pattern matching that find and locate do, unlike in the shell, in which wildcards do not match them. Therefore, a pattern ‘foobar’ can match a file name ‘foo3/bar’, and a pattern ‘./srsc’ can match a file name ‘./src/misc’.



        If you want to locate some files with the ‘locate’ command but don't need to see the full list you can use the ‘--limit’ option to see just a small number of results, or the ‘--count’ option to display only the total number of matches.




        To answer your question:



        find . -name "*.sw?" -type f





        share|improve this answer















        -name pattern
        Base of file name (the path with the leading directories
        removed) matches shell pattern pattern. Because the leading
        directories are removed, the file names considered for a match
        with -name will never include a slash, so `-name a/b' will
        never match anything (you probably need to use -path instead).
        A warning is issued if you try to do this, unless the
        environment variable POSIXLY_CORRECT is set. The
        metacharacters (`*', `?', and `[]') match a `.' at the start
        of the base name (this is a change in findutils-4.2.2; see
        section STANDARDS CONFORMANCE below). To ignore a directory
        and the files under it, use -prune; see an example in the
        description of -path. Braces are not recognised as being
        special, despite the fact that some shells including Bash
        imbue braces with a special meaning in shell patterns. The
        filename matching is performed with the use of the fnmatch(3)
        library function. Don't forget to enclose the pattern in
        quotes in order to protect it from expansion by the shell.


        It uses shell patterns not regex.



        Source: find(1)




        From the GNU manual under name:




        Here are ways to search for files whose name matches a certain pattern. See Shell Pattern Matching, for a description of the pattern arguments to these tests.




        2.1.4 Shell Pattern Matching



        find and locate can compare file names, or parts of file names, to shell patterns. A shell pattern is a string that may contain the following special characters, which are known as wildcards or metacharacters.



        You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.




        • *

          Matches any zero or more characters.


        • ?

          Matches any one character.


        • [string]

          Matches exactly one character that is a member of the string string. This is called a character class. As a shorthand, string may contain ranges, which consist of two characters with a dash between them. For example, the class ‘[a-z0-9_]’ matches a lowercase letter, a number, or an underscore. You can negate a class by placing a ‘!’ or ‘^’ immediately after the opening bracket. Thus, ‘[^A-Z@]’ matches any character except an uppercase letter or an at sign.




        • Removes the special meaning of the character that follows it. This works even in character classes.

          In the find tests that do shell pattern matching (‘-name’, ‘-wholename’, etc.), wildcards in the pattern will match a ‘.’ at the beginning of a file name. This is also the case for locate. Thus, ‘find -name '*macs'’ will match a file named .emacs, as will ‘locate '*macs'’.

        Slash characters have no special significance in the shell pattern matching that find and locate do, unlike in the shell, in which wildcards do not match them. Therefore, a pattern ‘foobar’ can match a file name ‘foo3/bar’, and a pattern ‘./srsc’ can match a file name ‘./src/misc’.



        If you want to locate some files with the ‘locate’ command but don't need to see the full list you can use the ‘--limit’ option to see just a small number of results, or the ‘--count’ option to display only the total number of matches.




        To answer your question:



        find . -name "*.sw?" -type f






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 10 hours ago

























        answered 12 hours ago









        Jesse_bJesse_b

        14.6k33574




        14.6k33574




















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









            draft saved

            draft discarded


















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












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











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














            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%2f515698%2fregex-and-name-directives-in-find%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

            Nidaros erkebispedøme

            Birsay

            Was Woodrow Wilson really a Liberal?Was World War I a war of liberals against authoritarians?Founding Fathers...