Find command: Exclude or Ignore Files (e.g. Ignore All Hidden .dot Files )

How do I ignore all hidden .dot files while searching for files using the find command? How do I ignore or exclude certain files while running Linux or UNIX find command?

The find command support standard UNIX regex to match, include or exclude files. You can write complex queries easily with regex while find command recursively descends the directory tree for each /file/to/path listed, evaluating an expression. The syntax is as follows:

## Basic syntax ##
find /dir/to/search/ -options -name 'regex' -action
find /dir/to/search/ -options -iname 'regex' -action
find /dir/to/search/ -type f -name 'regex' -print
find /dir/to/search/ -type f -name ( expression ) -print
 
## ---------------------------------------------------------------------- ##
## The -and operator is the logical AND operator                          ## 
find /dir/to/search/ -type f -name 'expression -and expression' -print
 
## ---------------------------------------------------------------------- ##
## The -or operator is the logical OR operator.  The expression evaluates ##
## to true if either the first or the second expression is true.          ##
find /dir/to/search/ -type f -name 'expression -or expression' -print

## Basic syntax ##
find /dir/to/search/ -options -name ‘regex’ -action
find /dir/to/search/ -options -iname ‘regex’ -action
find /dir/to/search/ -type f -name ‘regex’ -print
find /dir/to/search/ -type f -name ( expression ) -print ## ———————————————————————- ##
## The -and operator is the logical AND operator ##
find /dir/to/search/ -type f -name ‘expression -and expression’ -print ## ———————————————————————- ##
## The -or operator is the logical OR operator. The expression evaluates ##
## to true if either the first or the second expression is true. ##
find /dir/to/search/ -type f -name ‘expression -or expression’ -print

Examples: find command and logical operators

Find any file whose name ends with either ‘c’ or ‘asm’, enter:
$ find . -type f ( -iname "*.c" -or -iname "*.asm" )
OR find all *.conf and text file in /etc/ directory:
$ find . -type f ( -name "*.conf" -or -name "*.txt" ) -print
Sample outputs:

Fig.01: Linux find command exclude files command

The parentheses must be escaped with a backslash, “(” and “)“, to prevent them from being interpreted as special shell characters. The -type f option force to only search files and not directories. The -or operator either find .c or .asm file.

Understanding find command operators

Operators build a complex expression from tests and actions. The operators are, in order of decreasing precedence:

( expr ) Force precedence. True if expr is true
expr -not expr
! expr
True if expr is false. In some shells, it is necessary to protect the ‘!’ from shell interpretation by quoting it.
expr1 -and expr2 expr2 is not evaluated if expr1 is false.
expr1 -or expr2 expr2 is not evaluated if expr1 is true.
WARNING! The ‘-or’, ‘-and’, and ‘-not’ operator are not available on all versions of find. Usually GNU find supports all options. Refer your local find command man page for more information.

How do I ignore hidden .dot files while searching for files?

Find *.txt file but ignore hidden .txt file such as .vimrc or .data.txt file:
$ find . -type f ( -iname "*.txt" ! -iname ".*" )
Find all .dot files but ignore .htaccess file:
$ find . -type f ( -iname ".*" ! -iname ".htaccess" )

Say hello to -path option

This option return true if the pathname being examined matches pattern. For example, find all *.txt files in the current directory but exclude ./Movies/, ./Downloads/, and ./Music/ folders:

cd $HOME
find . -type f -name "*.txt" ! -path "./Movies/*" ! -path "./Downloads/*" ! -path "./Music/*" 
 
## add -ls option to get ls -l kind of output ##
find . -type f -name "*.txt" ! -path "./Movies/*" ! -path "./Downloads/*" ! -path "./Music/*" -ls

cd $HOME
find . -type f -name "*.txt" ! -path "./Movies/*" ! -path "./Downloads/*" ! -path "./Music/*" ## add -ls option to get ls -l kind of output ##
find . -type f -name "*.txt" ! -path "./Movies/*" ! -path "./Downloads/*" ! -path "./Music/*" -ls

Sample outputs:

./.config/gcloud/legacy_credentials/[email protected]/gaejava.txt
./adsl-ip-info.txt
./Desktop/Bills-PDF/codex.txt
./Desktop/Bills-PDF/2015.txt
./Desktop/Bills-PDF/receipt-05-OCT-14 10-08 AM.txt
./Desktop/Bills-PDF/untitled.txt
./Desktop/Bills-PDF/wufoo/Give Us Feedback for SXI LLC.txt
./Desktop/tutorials.txt
./Desktop/untitled 4.txt
./Documents/centos-7-selinux.txt
./Documents/hhmv-domain.txt
./Documents/ip.txt
./Documents/pfsec-ipsec.txt
./Documents/smem-rhel-centos.txt
./Documents/3m.txt

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.