Ubuntu – How to exclude/ignore hidden files and directories in a wildcard-embedded “find” search

command linefind

Starting from (notice the wildcards before and after "some text")

find . -type f -name '*some text*'

how can one exclude/ignore all hidden files and directories?

I've already been googling for far too long, came across some -prune and ! (exclamation mark) parameters, but no fitting (and parsimonious) example which just worked.

Piping | to grep would be an option and I'd also welcome examples of that; but primarily I'm interested in a brief one-liner (or a couple of stand-alone one-liners, illustrating different ways of achieving the same command-line goal) just using find.

ps: Find files in linux and exclude specific directories seems closely related, but a) is not accepted yet and b) is related-but-different-and-distinct, but c) may provide inspiration and help pinpoint the confusion!

Edit

find . \( ! -regex '.*/\..*' \) -type f -name "whatever", works. The regex looks for "anything, then a slash, then a dot, then anything" (i.e. all hidden files and folders including their subfolders), and the "!" negates the regex.

Best Answer

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