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












2
















This question already has an answer here:




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

    3 answers




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















marked as duplicate by Christopher, Jeff Schaller, Mr Shunz, Thomas, msp9011 Jan 25 at 10:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Also: How to use find command to search for multiple extensions

    – ilkkachu
    Jan 24 at 22:18
















2
















This question already has an answer here:




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

    3 answers




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















marked as duplicate by Christopher, Jeff Schaller, Mr Shunz, Thomas, msp9011 Jan 25 at 10:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Also: How to use find command to search for multiple extensions

    – ilkkachu
    Jan 24 at 22:18














2












2








2









This question already has an answer here:




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

    3 answers




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

















This question already has an answer here:




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

    3 answers




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"




This question already has an answer here:




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

    3 answers








find regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 24 at 22:18









ilkkachu

60k997169




60k997169










asked Jan 24 at 14:02









BrowncoatOkieBrowncoatOkie

816




816




marked as duplicate by Christopher, Jeff Schaller, Mr Shunz, Thomas, msp9011 Jan 25 at 10:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Christopher, Jeff Schaller, Mr Shunz, Thomas, msp9011 Jan 25 at 10:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Also: How to use find command to search for multiple extensions

    – ilkkachu
    Jan 24 at 22:18



















  • Also: How to use find command to search for multiple extensions

    – ilkkachu
    Jan 24 at 22:18

















Also: How to use find command to search for multiple extensions

– ilkkachu
Jan 24 at 22:18





Also: How to use find command to search for multiple extensions

– ilkkachu
Jan 24 at 22:18










2 Answers
2






active

oldest

votes


















13














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.



-o has lower precedence than “and”, whether explicit (-a) or implicit; if you’re combining operators you might need to wrap the “or” part with parentheses:



find . ( -name "*.pem" -o -name "*.crt" ) -print


In my tests this is significantly faster than using a regular expression, as you might expect (regular expressions are more expensive to test than globs, and -regex tests the full path, not only the file name as -name does).






share|improve this answer


























  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    Jan 24 at 14:16



















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
    Jan 24 at 14:16






  • 3





    ".*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
    Jan 24 at 14:17






  • 3





    @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
    Jan 24 at 14:49













  • Regular-expression matching is a non-standard extension provided by some, but not necessarily all, implementations of find.

    – chepner
    Jan 24 at 20:45











  • @TavianBarnes that has already been mentioned by Kusalananda above]

    – phuclv
    Jan 29 at 14:32


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









13














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.



-o has lower precedence than “and”, whether explicit (-a) or implicit; if you’re combining operators you might need to wrap the “or” part with parentheses:



find . ( -name "*.pem" -o -name "*.crt" ) -print


In my tests this is significantly faster than using a regular expression, as you might expect (regular expressions are more expensive to test than globs, and -regex tests the full path, not only the file name as -name does).






share|improve this answer


























  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    Jan 24 at 14:16
















13














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.



-o has lower precedence than “and”, whether explicit (-a) or implicit; if you’re combining operators you might need to wrap the “or” part with parentheses:



find . ( -name "*.pem" -o -name "*.crt" ) -print


In my tests this is significantly faster than using a regular expression, as you might expect (regular expressions are more expensive to test than globs, and -regex tests the full path, not only the file name as -name does).






share|improve this answer


























  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    Jan 24 at 14:16














13












13








13







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.



-o has lower precedence than “and”, whether explicit (-a) or implicit; if you’re combining operators you might need to wrap the “or” part with parentheses:



find . ( -name "*.pem" -o -name "*.crt" ) -print


In my tests this is significantly faster than using a regular expression, as you might expect (regular expressions are more expensive to test than globs, and -regex tests the full path, not only the file name as -name does).






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.



-o has lower precedence than “and”, whether explicit (-a) or implicit; if you’re combining operators you might need to wrap the “or” part with parentheses:



find . ( -name "*.pem" -o -name "*.crt" ) -print


In my tests this is significantly faster than using a regular expression, as you might expect (regular expressions are more expensive to test than globs, and -regex tests the full path, not only the file name as -name does).







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 24 at 15:12

























answered Jan 24 at 14:04









Stephen KittStephen Kitt

173k24396471




173k24396471













  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    Jan 24 at 14:16



















  • Even better. Simpler and easier to remember.

    – BrowncoatOkie
    Jan 24 at 14:16

















Even better. Simpler and easier to remember.

– BrowncoatOkie
Jan 24 at 14:16





Even better. Simpler and easier to remember.

– BrowncoatOkie
Jan 24 at 14:16













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
    Jan 24 at 14:16






  • 3





    ".*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
    Jan 24 at 14:17






  • 3





    @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
    Jan 24 at 14:49













  • Regular-expression matching is a non-standard extension provided by some, but not necessarily all, implementations of find.

    – chepner
    Jan 24 at 20:45











  • @TavianBarnes that has already been mentioned by Kusalananda above]

    – phuclv
    Jan 29 at 14:32
















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
    Jan 24 at 14:16






  • 3





    ".*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
    Jan 24 at 14:17






  • 3





    @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
    Jan 24 at 14:49













  • Regular-expression matching is a non-standard extension provided by some, but not necessarily all, implementations of find.

    – chepner
    Jan 24 at 20:45











  • @TavianBarnes that has already been mentioned by Kusalananda above]

    – phuclv
    Jan 29 at 14:32














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 Jan 24 at 14:02









BrowncoatOkieBrowncoatOkie

816




816













  • 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
    Jan 24 at 14:16






  • 3





    ".*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
    Jan 24 at 14:17






  • 3





    @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
    Jan 24 at 14:49













  • Regular-expression matching is a non-standard extension provided by some, but not necessarily all, implementations of find.

    – chepner
    Jan 24 at 20:45











  • @TavianBarnes that has already been mentioned by Kusalananda above]

    – phuclv
    Jan 29 at 14:32



















  • 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
    Jan 24 at 14:16






  • 3





    ".*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
    Jan 24 at 14:17






  • 3





    @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
    Jan 24 at 14:49













  • Regular-expression matching is a non-standard extension provided by some, but not necessarily all, implementations of find.

    – chepner
    Jan 24 at 20:45











  • @TavianBarnes that has already been mentioned by Kusalananda above]

    – phuclv
    Jan 29 at 14:32

















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
Jan 24 at 14:16





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
Jan 24 at 14:16




3




3





".*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
Jan 24 at 14:17





".*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
Jan 24 at 14:17




3




3





@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
Jan 24 at 14:49







@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
Jan 24 at 14:49















Regular-expression matching is a non-standard extension provided by some, but not necessarily all, implementations of find.

– chepner
Jan 24 at 20:45





Regular-expression matching is a non-standard extension provided by some, but not necessarily all, implementations of find.

– chepner
Jan 24 at 20:45













@TavianBarnes that has already been mentioned by Kusalananda above]

– phuclv
Jan 29 at 14:32





@TavianBarnes that has already been mentioned by Kusalananda above]

– phuclv
Jan 29 at 14:32



Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

張江高科駅