What options do I need to use with find
to exclude hidden files?
8 Answers
I found this here:
find . \( ! -regex '.*/\..*' \) -type f -name "whatever"
-
-
@grawity I just found that, I don't know entirely how it works. Would yours not only hide hidden files, but hidden directories and all their sub-content and hidden files in subfolders?– JarvinJun 16, 2010 at 16:05
-
5
-
@grawity Ya, I guess I made an assumption about what the OP wanted... Your -name solution is probably the closest to what they were asking for.– JarvinJun 16, 2010 at 18:07
-
@grawity&Dan: Isn't it ( !-path '^.*' ) ?? your solutions will ignore any file that has a '.' anywhere in the file name like a.exe, b.out etc.... Jun 30, 2011 at 8:27
It seems negation glob pattern is not well known. So you can use:
find . -name "[!.]*"
-
note that this will still find non-hidden files in hidden directories, which may or may not be what you want. Apr 26 at 9:14
This doesn't answer your question, but for the task of finding non-hidden files I like to let find find all the files then filter with grep.
find . -type f | grep -v '/\.'
Similar to your approach but perhaps a bit simpler.
Try the following find
usage:
find . -type f -not -path '*/\.*'
Which would ignore all the hidden files (files and directories starting with a dot).
fd
Use fd
, a simple, much faster and user-friendly alternative to find
. By default, it:
- Ignores hidden directories and files, by default.
- Ignores patterns from your
.gitignore
, by default.
Check the Benchmark analysis.
If you aims is to find
and grep
, ripgrep
does exclude hidden files by default, e.g.
rg --files
--files
Print each file that would be searched without actually performing the search.
To find hidden files:
find -name '[.]*'
To find visible files:
find -name '[!.]*'
It is that simple.
I wrote a script called findnh
which I believe handles certain edge cases better than the answers to this question that I've been able to find on the web.
#!/bin/bash
declare -a paths
while [ $# -ne 0 ]; do
case "$1" in -*) break ;; esac
paths+=("$1")
shift
done
find "${paths[@]}" \( -name . -o -name .. -o \! \( -name '.*' -prune \) \) "$@"
For example, you can find non-hidden files and directories inside of an explicitly-specified hidden directory with a command like findnh ~/.hiddendir/
, which will show ~/.hiddendir/file
but not ~/.hiddendir/.superhiddenfile
.
-
1Nice bit of coding. Except, when I try
findnh ~/.hiddendir/
, I get nothing. Other than that, how is this different from! -path '*/.*'
andfind … | grep -v '/\.'
? Oct 22, 2014 at 16:42
ls
unless specifically requested: they are completely ordinary files in every respect, its just thatls
lets you ignore them by default..hidden/visible.txt
?