How to exclude/ignore hidden files and directories in a wildcard-embedded "find" search?
This prints all files that are descendants of your directory, skipping hidden files and directories:
find . -not -path '*/\.*'
So if you're looking for a file with some text
in its name, and you want to skip hidden files and directories, run:
find . -not -path '*/\.*' -type f -name '*some text*'
Explanation:
The -path
option runs checks a pattern against the entire path string. *
is a wildcard, /
is a directory separator, \.
is a dot (it has to be escaped to avoid special meaning), and *
is another wildcard. -not
means don't select files that match this test.
I don't think that find
is smart enough to avoid recursively searching hidden directories in the previous command, so if you need speed, use -prune
instead, like this:
find . -type d -path '*/\.*' -prune -o -not -name '.*' -type f -name '*some text*' -print
This is one of the few means of excludes dot-files that also works correctly on BSD, Mac and Linux:
find "$PWD" -name ".*" -prune -o -print
$PWD
print the full path to the current directory so that the path does not start with./
-name ".*" -prune
matches any files or directories that start with a dot and then don't descend-o -print
means print the file name if the previous expression did not match anything. Using-print
or-print0
causes all other expressions to not print by default.
find $DIR -not -path '*/\.*' -type f \( ! -iname ".*" \)
Excludes all hidden directories, and hidden files under $DIR