How can I do an “or” search with find?
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
add a comment |
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
Possible duplicate of How to search with GNU find for several file types at a time?
– phuclv
16 mins ago
add a comment |
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
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
linux find regular-expression
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
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 (GNUfind
'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
add a comment |
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!
Run a couple of finds withtime
at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth usinglocate
.
– 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 (GNUfind
'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
add a comment |
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.
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 (GNUfind
'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
add a comment |
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.
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.
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 (GNUfind
'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
add a comment |
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 (GNUfind
'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
add a comment |
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!
Run a couple of finds withtime
at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth usinglocate
.
– 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
add a comment |
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!
Run a couple of finds withtime
at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth usinglocate
.
– 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
add a comment |
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!
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!
answered 1 hour ago
BrowncoatOkieBrowncoatOkie
786
786
Run a couple of finds withtime
at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth usinglocate
.
– 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
add a comment |
Run a couple of finds withtime
at the start to analyse whether you're faster using regex or wildcards. If you're searching over files might be worth usinglocate
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Possible duplicate of How to search with GNU find for several file types at a time?
– phuclv
16 mins ago