How do I show all hidden & invisible files on the command line (both .files and invisible flagged files)?

Is there a way to show only the hidden and invisible files in a directory using the command line?

For instance ls -a | grep "^\." will show all the files hidden by a period as the first character, but what about files hidden with the invisible flag or listed in .hidden (mostly deprecated in practice, don't worry about that one so much.)?


Solution 1:

If the files are indexed by Spotlight and files in subdirectories can be included, you could use mdfind:

mdfind kMDItemFSInvisible=1 -onlyin .

Or test for both GetFileInfo -av (attribute invisible) and if the name starts with a period:

shopt -s dotglob nullglob
for f in *; do [[ $(GetFileInfo -av "$f") = 1 || $f = .* ]] && echo "$f"; done

GetFileInfo is part of the command line tools package that can be downloaded from Xcode's preferences or from developer.apple.com/downloads.

Different ways to list only files that start with a period:

shopt -s nullglob; printf %s\\n .[^.]* ..?*
shopt -s dotglob nullglob; GLOBIGNORE='. ..'; printf %s\\n *
ls -a | grep -E '^(\.[^.]|\.\.[^$])'