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.

Find command exclude or ignore files syntax

The syntax is as follows:

## Basic syntax ##
find /dir/to/search/ -options -name 'pattern' -action
find /dir/to/search/ -options -iname 'pattern' -action
find /dir/to/search/ -type f -name 'pattern' -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" \)
In this example, find all *.conf and (.txt) text files in the /etc/ directory:
$ find . -type f \( -name "*.conf" -or -name "*.txt" \) -print

Fig.01: Linux find command exclude files command

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 find 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

Sample outputs:

./Desktop/Bills-PDF/receipt-05-OCT-14 10-08 AM.txt
./Desktop/Bills-PDF/wufoo/Give Us Feedback for nixCraft.txt
./Desktop/untitled 4.txt


You learned how to ignore particular filenames while using find command on Linux or Unix-like systems.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 23 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
23 comments… add one
  • scott Aug 8, 2009 @ 1:49

    Thanks for the ! example!!

  • vapere Jul 15, 2010 @ 13:50

    Hi! Cna u help me?
    I need find all *.css file but exclude one file /lang/en.css, but include /1/lang/en.css

    • wajrou Nov 4, 2010 @ 20:29

      find . -path ‘*/lang/en.css’ -prune -o -name ‘*.css’ -print; find . -path ‘*/1/lang/en.css’ -print

  • mactruck Apr 26, 2012 @ 22:28

    can you help me?
    I need to find all files older than 500 days.

    this is my code so far and I get an error saying -iname is not a valid option.

    find . -type f \( ! -iname ".*" \) -mtime +500 -exec ls {} \;
    • bsod Jan 7, 2014 @ 8:36

      find . -type f -not -name ‘.*’ -mtime +500 -exec ls {} \;

  • Nikesh Jauhari May 2, 2012 @ 20:43

    thanks for excellent tips on find command

  • saireddysateesh May 30, 2012 @ 11:17

    i have some issue
    having a directory LOG. in that so many files and directories are there. i want to find and delete the file which starts with PS. but it should not check sub directories.

  • amol Jul 13, 2012 @ 7:08

    I have to delete the files modified yesterday so I can keep only today files (which I download daily ) in current directory how do I do that ?

  • Gary Dec 31, 2014 @ 18:25

    Is there a way to get a list of “.c” files and grep the contents only in those files for a keyword?

    I tried
    find . -type f \( -iname "*.c" \) |grep -i -r "keyword"

    It seems like grep is still going through all the files and not just the “.c” files.

    • Richard Mason Feb 27, 2015 @ 13:50
      find . -name "*.c" -exec grep -ir "keyword" {} ";"
  • pankaj Feb 9, 2015 @ 3:15

    How can i find a last 5 min created file in a folder not the sub folder like.

    i serach a file inside folder /home/pankaj but not in /home/pankaj/test/test1

    • Tommy Oct 15, 2015 @ 8:19

      You can not check creation time but you can check for last change like this:

      find /home/pankaj -maxdepth 1 -cmin -5 -type f

      The “-maxdepth 1” keeps find from looking in subfolders but will check the subfolder itself, that’s why you use “-type f” to only look for files, “-cmin” specifies the minutes since last change, use +5 to get files older than 5 min.

  • Dominik Feb 21, 2016 @ 14:32

    Hi Pankaj, i was faced same issue, i search on internet and found this article, its really helpful, i succeed that with Long Path Tool, you can try

  • Arul May 20, 2016 @ 16:44

    In find command i am not entered file name, so it’s showing error message.

    Eg: find . -name

    error like “parameter not entered”

    I want to discard this error

    I used the Error handling method like “2>/dev/null
    then also error is showing.

    if any one knows reply me.

  • donnghi Jul 3, 2016 @ 0:30

    Your tutorial is very clear and helpful.
    Thank so much!

  • SHAMEER Apr 3, 2017 @ 5:58

    find . -size +20M -type f \( ! -iname "*.ppm" ! -iname "*.pic" ! -iname "*.v" \)

    The above command can show all files with greater than 20 Mega Bytes and not having following extension .ppm, .pic, .v

  • Javeed Apr 6, 2017 @ 8:44

    Am trying to find some files which are created 6 months ago Or 180 days back. and delete all, but except the files which contains “1545” in the filename any help please.
    I could able to fine all the files but could not able to exclude them
    find . -type f -not -name .* -mtime +180 -exec ls {} \;

    • Javeed Apr 6, 2017 @ 9:18

      Found solution
      find . -type f ! -iname "*1545*" -not -name ".*" -mtime +180 -exec rm -f {} \;

      all because of your examples above.

  • kevin Aug 12, 2020 @ 18:46

    I was looking for a way to skip the . and .. directory and found it here. Thanks.

  • CB Aug 15, 2020 @ 22:12

    -name ‘regex’ is wrong. For regex there’s -regex option.

  • eric Feb 27, 2021 @ 2:17

    Are the parentheses required for file or directory name/pattern?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum