≡ Menu

Find command: Exclude / Ignore Files ( Ignore Hidden .dot Files )

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

A. Find command support standard UNIX regex to match or exclude files. You can write complex queries easily with regex.

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" \)
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
-not expr
True if expr is false. In some shells, it is necessary to protect the ‘!’ from shell interpretation by quoting it.
expr1 -and expr2 And; expr2 is not evaluated if expr1 is false.
expr1 -or expr2 Or; 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" \)

{ 12 comments… add one }

  • scott August 8, 2009, 1:49 am

    Thanks for the ! example!!

  • vapere July 15, 2010, 1:50 pm

    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 November 4, 2010, 8:29 pm

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

  • mactruck April 26, 2012, 10:28 pm

    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 January 7, 2014, 8:36 am

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

  • Nikesh Jauhari May 2, 2012, 8:43 pm

    thanks for excellent tips on find command

  • saireddysateesh May 30, 2012, 11:17 am

    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 July 13, 2012, 7:08 am

    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 December 31, 2014, 6:25 pm

    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 February 27, 2015, 1:50 pm
      find . -name "*.c" -exec grep -ir "keyword" {} ";"
  • pankaj February 9, 2015, 3:15 am

    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 October 15, 2015, 8:19 am

      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.

Leave a Comment

   Tagged with: , , , , , , , , , , , , , ,