How can I do an “or” search with find?












2















Essentially, I want to know how to run 2 (or more) find commands in one - an "or" search rather than an "and":



find . -name "*.pem"
find . -name "*.crt"









share|improve this question























  • Possible duplicate of How to search with GNU find for several file types at a time?

    – phuclv
    16 mins ago
















2















Essentially, I want to know how to run 2 (or more) find commands in one - an "or" search rather than an "and":



find . -name "*.pem"
find . -name "*.crt"









share|improve this question























  • Possible duplicate of How to search with GNU find for several file types at a time?

    – phuclv
    16 mins ago














2












2








2








Essentially, I want to know how to run 2 (or more) find commands in one - an "or" search rather than an "and":



find . -name "*.pem"
find . -name "*.crt"









share|improve this question














Essentially, I want to know how to run 2 (or more) find commands in one - an "or" search rather than an "and":



find . -name "*.pem"
find . -name "*.crt"






linux find regular-expression






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









BrowncoatOkieBrowncoatOkie

786




786













  • Possible duplicate of How to search with GNU find for several file types at a time?

    – phuclv
    16 mins ago



















  • Possible duplicate of How to search with GNU find for several file types at a time?

    – phuclv
    16 mins ago

















Possible duplicate of How to search with GNU find for several file types at a time?

– phuclv
16 mins ago





Possible duplicate of How to search with GNU find for several file types at a time?

– phuclv
16 mins ago










2 Answers
2






active

oldest

votes


















6














find’s “or” operator is -o:



find . -name "*.pem" -o -name "*.crt"


It is short-circuiting, i.e. the second part will only be evaluated if the first part is false: a file which matches *.pem won’t be tested against *.crt.



In my tests this is significantly faster than using a regular expression.






share|improve this answer


























  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    53 mins ago











  • To group the tests (for later actions or test): find . ( -name ... -o -name ... ) more here, where ( may also be '(' or "(" etc.

    – Kusalananda
    21 mins ago











  • It's quicker in part because regular expressions (GNU find's -regex) are applied to the whole pathname, not just the filename. Regular expressions are also more complex to match ("heavier" engine to run them).

    – Kusalananda
    16 mins ago





















1














While I was typing up this question, it occurred to me that find uses globbing rather than regex by default. But I bet there's a way to use regex!



Sure enough...I had to change the regextype to use posix-extended but that got me what I wanted.



find . -regextype posix-extended -regex ".*pem|.*crt"


Qaplah!






share|improve this answer
























  • Run a couple of finds with time at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth using locate.

    – pbhj
    53 mins ago






  • 2





    ".*pem|.*crt" finds a name that ends with pem or crt, not a file with those extensions. You'll need .*.pem|.*.crt for that. But using regex for this isn't a good method anyway

    – phuclv
    52 mins ago






  • 1





    @phuclv Actually, since the expression is not anchored to the end of the pathname, it may match anywhere, even in a parent directory name. This is because -regex is applied to the whole pathname and because unanchored regular expressions may match anywhere in the given string.

    – Kusalananda
    19 mins ago













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%2f496469%2fhow-can-i-do-an-or-search-with-find%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









6














find’s “or” operator is -o:



find . -name "*.pem" -o -name "*.crt"


It is short-circuiting, i.e. the second part will only be evaluated if the first part is false: a file which matches *.pem won’t be tested against *.crt.



In my tests this is significantly faster than using a regular expression.






share|improve this answer


























  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    53 mins ago











  • To group the tests (for later actions or test): find . ( -name ... -o -name ... ) more here, where ( may also be '(' or "(" etc.

    – Kusalananda
    21 mins ago











  • It's quicker in part because regular expressions (GNU find's -regex) are applied to the whole pathname, not just the filename. Regular expressions are also more complex to match ("heavier" engine to run them).

    – Kusalananda
    16 mins ago


















6














find’s “or” operator is -o:



find . -name "*.pem" -o -name "*.crt"


It is short-circuiting, i.e. the second part will only be evaluated if the first part is false: a file which matches *.pem won’t be tested against *.crt.



In my tests this is significantly faster than using a regular expression.






share|improve this answer


























  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    53 mins ago











  • To group the tests (for later actions or test): find . ( -name ... -o -name ... ) more here, where ( may also be '(' or "(" etc.

    – Kusalananda
    21 mins ago











  • It's quicker in part because regular expressions (GNU find's -regex) are applied to the whole pathname, not just the filename. Regular expressions are also more complex to match ("heavier" engine to run them).

    – Kusalananda
    16 mins ago
















6












6








6







find’s “or” operator is -o:



find . -name "*.pem" -o -name "*.crt"


It is short-circuiting, i.e. the second part will only be evaluated if the first part is false: a file which matches *.pem won’t be tested against *.crt.



In my tests this is significantly faster than using a regular expression.






share|improve this answer















find’s “or” operator is -o:



find . -name "*.pem" -o -name "*.crt"


It is short-circuiting, i.e. the second part will only be evaluated if the first part is false: a file which matches *.pem won’t be tested against *.crt.



In my tests this is significantly faster than using a regular expression.







share|improve this answer














share|improve this answer



share|improve this answer








edited 59 mins ago

























answered 1 hour ago









Stephen KittStephen Kitt

168k24379456




168k24379456













  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    53 mins ago











  • To group the tests (for later actions or test): find . ( -name ... -o -name ... ) more here, where ( may also be '(' or "(" etc.

    – Kusalananda
    21 mins ago











  • It's quicker in part because regular expressions (GNU find's -regex) are applied to the whole pathname, not just the filename. Regular expressions are also more complex to match ("heavier" engine to run them).

    – Kusalananda
    16 mins ago





















  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    53 mins ago











  • To group the tests (for later actions or test): find . ( -name ... -o -name ... ) more here, where ( may also be '(' or "(" etc.

    – Kusalananda
    21 mins ago











  • It's quicker in part because regular expressions (GNU find's -regex) are applied to the whole pathname, not just the filename. Regular expressions are also more complex to match ("heavier" engine to run them).

    – Kusalananda
    16 mins ago



















Even better. Simpler and easier to remember.

– BrowncoatOkie
53 mins ago





Even better. Simpler and easier to remember.

– BrowncoatOkie
53 mins ago













To group the tests (for later actions or test): find . ( -name ... -o -name ... ) more here, where ( may also be '(' or "(" etc.

– Kusalananda
21 mins ago





To group the tests (for later actions or test): find . ( -name ... -o -name ... ) more here, where ( may also be '(' or "(" etc.

– Kusalananda
21 mins ago













It's quicker in part because regular expressions (GNU find's -regex) are applied to the whole pathname, not just the filename. Regular expressions are also more complex to match ("heavier" engine to run them).

– Kusalananda
16 mins ago







It's quicker in part because regular expressions (GNU find's -regex) are applied to the whole pathname, not just the filename. Regular expressions are also more complex to match ("heavier" engine to run them).

– Kusalananda
16 mins ago















1














While I was typing up this question, it occurred to me that find uses globbing rather than regex by default. But I bet there's a way to use regex!



Sure enough...I had to change the regextype to use posix-extended but that got me what I wanted.



find . -regextype posix-extended -regex ".*pem|.*crt"


Qaplah!






share|improve this answer
























  • Run a couple of finds with time at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth using locate.

    – pbhj
    53 mins ago






  • 2





    ".*pem|.*crt" finds a name that ends with pem or crt, not a file with those extensions. You'll need .*.pem|.*.crt for that. But using regex for this isn't a good method anyway

    – phuclv
    52 mins ago






  • 1





    @phuclv Actually, since the expression is not anchored to the end of the pathname, it may match anywhere, even in a parent directory name. This is because -regex is applied to the whole pathname and because unanchored regular expressions may match anywhere in the given string.

    – Kusalananda
    19 mins ago


















1














While I was typing up this question, it occurred to me that find uses globbing rather than regex by default. But I bet there's a way to use regex!



Sure enough...I had to change the regextype to use posix-extended but that got me what I wanted.



find . -regextype posix-extended -regex ".*pem|.*crt"


Qaplah!






share|improve this answer
























  • Run a couple of finds with time at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth using locate.

    – pbhj
    53 mins ago






  • 2





    ".*pem|.*crt" finds a name that ends with pem or crt, not a file with those extensions. You'll need .*.pem|.*.crt for that. But using regex for this isn't a good method anyway

    – phuclv
    52 mins ago






  • 1





    @phuclv Actually, since the expression is not anchored to the end of the pathname, it may match anywhere, even in a parent directory name. This is because -regex is applied to the whole pathname and because unanchored regular expressions may match anywhere in the given string.

    – Kusalananda
    19 mins ago
















1












1








1







While I was typing up this question, it occurred to me that find uses globbing rather than regex by default. But I bet there's a way to use regex!



Sure enough...I had to change the regextype to use posix-extended but that got me what I wanted.



find . -regextype posix-extended -regex ".*pem|.*crt"


Qaplah!






share|improve this answer













While I was typing up this question, it occurred to me that find uses globbing rather than regex by default. But I bet there's a way to use regex!



Sure enough...I had to change the regextype to use posix-extended but that got me what I wanted.



find . -regextype posix-extended -regex ".*pem|.*crt"


Qaplah!







share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









BrowncoatOkieBrowncoatOkie

786




786













  • Run a couple of finds with time at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth using locate.

    – pbhj
    53 mins ago






  • 2





    ".*pem|.*crt" finds a name that ends with pem or crt, not a file with those extensions. You'll need .*.pem|.*.crt for that. But using regex for this isn't a good method anyway

    – phuclv
    52 mins ago






  • 1





    @phuclv Actually, since the expression is not anchored to the end of the pathname, it may match anywhere, even in a parent directory name. This is because -regex is applied to the whole pathname and because unanchored regular expressions may match anywhere in the given string.

    – Kusalananda
    19 mins ago





















  • Run a couple of finds with time at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth using locate.

    – pbhj
    53 mins ago






  • 2





    ".*pem|.*crt" finds a name that ends with pem or crt, not a file with those extensions. You'll need .*.pem|.*.crt for that. But using regex for this isn't a good method anyway

    – phuclv
    52 mins ago






  • 1





    @phuclv Actually, since the expression is not anchored to the end of the pathname, it may match anywhere, even in a parent directory name. This is because -regex is applied to the whole pathname and because unanchored regular expressions may match anywhere in the given string.

    – Kusalananda
    19 mins ago



















Run a couple of finds with time at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth using locate.

– pbhj
53 mins ago





Run a couple of finds with time at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth using locate.

– pbhj
53 mins ago




2




2





".*pem|.*crt" finds a name that ends with pem or crt, not a file with those extensions. You'll need .*.pem|.*.crt for that. But using regex for this isn't a good method anyway

– phuclv
52 mins ago





".*pem|.*crt" finds a name that ends with pem or crt, not a file with those extensions. You'll need .*.pem|.*.crt for that. But using regex for this isn't a good method anyway

– phuclv
52 mins ago




1




1





@phuclv Actually, since the expression is not anchored to the end of the pathname, it may match anywhere, even in a parent directory name. This is because -regex is applied to the whole pathname and because unanchored regular expressions may match anywhere in the given string.

– Kusalananda
19 mins ago







@phuclv Actually, since the expression is not anchored to the end of the pathname, it may match anywhere, even in a parent directory name. This is because -regex is applied to the whole pathname and because unanchored regular expressions may match anywhere in the given string.

– Kusalananda
19 mins ago




















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%2f496469%2fhow-can-i-do-an-or-search-with-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

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana