Use multiple conditions in find regex in shell

EDITED: I need to use -E for extended regex.

I have a folder with this files (just an example):
directory structure

I'm trying to find all files that:

  1. Start and end with #. (e.g #hi.h#)
  2. End with ~. (e.g file.txt~)

I can find 1 condition files or 2 condition files, but i can't combine both in one regex.

$ find . -regex "./.*~"
./lld~

$ find . -regex "./#.*#"
./#x2#
./#x#

But this command is not working:

$ find . -regex "./(.*~$)|(#.*#)"

What am I doing wrong? how I can combine these regexes?


find . -regex "\./#.*#\|\./.*~"

Does this work for you?


I need to use -E for extended regex.

Invoke find . -regextype help to learn available options. GNU find in my Debian supports few. This works:

find . -regextype posix-extended -regex '\./(.*~$|#.*#)'

Note I have debugged and simplified the regex a little (\./.*~$|\./#.*# would also work). Other options that work for me in this particular case: posix-egrep, egrep, posix-awk, awk, gnu-awk.


This command:

find . -regex '\./#.*#\|\./.*~'

where | is escaped works for me. Credits to the other answer. Proper escaping of ( and ) makes the following work as well:

find . -regex '\./\(.*~$\|#.*#\)'

without relying on extended regular expressions.


You don't have to compact the two expressions into one. If these work:

find . -regex '\./.*~'
find . -regex '\./#.*#'

then you can get files matching one regex or the other this way:

find . -regex '\./.*~' -o -regex '\./#.*#'

Be warned: Why does find in Linux skip expected results when -o is used? If you want to add more tests/actions before and/or after then you don't want this:

find . -test1 -test2 -regex '\./.*~' -o -regex '\./#.*#' -test3 …

but this:

find . -test1 -test2 '(' -regex '\./.*~' -o -regex '\./#.*#' ')' -test3 …