Search based on name or regular expression match

The -name argument specifies a selection pattern for the name. The -name argument accepts both glob-style wildcards and regular expressions. In the following example, '*.txt' matches all the file or folder names ending with .txt and prints them.

Note the single quotes around *.txt. The shell will expand glob wildcards with no quotes or using double-quotes ( "). The single quotes prevent the shell from expanding the *.txt and passes that string to the find command.
$ find /home/slynux -name '*.txt' -print

The find command has an option -iname (ignore case), which is similar to -name, but it matches filenames regardless of case.

Consider the following example:

$ ls
example.txt  EXAMPLE.txt  file.txt
$ find . -iname "example*" -print
./example.txt
./EXAMPLE.txt

The find command supports logical operations with the selection options. The   -a  and     -and options perform a logical AND, while the -o and -or option perform a logical OR.

$ ls
new.txt  some.jpg  text.pdf   stuff.png
$ find . \( -name '*.txt' -o -name '*.pdf' \) -print
./text.pdf
./new.txt

The previous command will print all the .txt and .pdf files, since the find command matches both .txt and .pdf files. \( and \) are used to treat -name "*.txt" -o -name "*.pdf" as a single unit.

The following command demonstrates using the -and operator to select only the file that starts with an s and has an e in the name somewhere.

$ find . \( -name '*e*' -and -name 's*' \) 
./some.jpg

The -path argument restricts the match to files that match a path as well as a name. For example, $ find /home/users -path '*/slynux/*' -name '*.txt' -print will find /home/users/slynux/readme.txt, but not /home/users/slynux.txt.

The -regex argument is similar to -path, but -regex matches the file paths based on regular expressions.

Regular expressions are more complex than glob wildcards and support more precise pattern matching. A typical example of text matching with regular expressions is to recognize all e-mail addresses. An e-mail address takes the name@host.root form. It can be generalized as [a-z0-9]+@[a-z0-9]+\.[a-z0-9]+. The characters inside the square brackets represent a set of characters. In this case, a-z and 0-9 The + sign signifies that the previous class of characters can occur one or more times. A period is a single character wildcard (like a ? in glob wildcards), so it must be escaped with a backslash to match an actual dot in the e-mail address. So, this regular expression translates to 'a sequence of letters or numbers, followed by an @, followed by a sequence of letters or numbers, followed by a period, and ending with a sequence of letters or numbers'. See the Using regular expressions  recipe in Chapter 4, Texting and Driving  for more details.

This command matches the .py or .sh files:

$ ls
new.PY  next.jpg  test.py script.sh
$ find . -regex '.*\.(py\|sh\)$'
./test.py
script.sh

The -iregex option ignores the case for regular expression matches.

Consider this example:

$ find . -iregex '.*\(\.py\|\.sh\)$'
./test.py
./new.PY
./script.sh