Showing All Hidden (dot) Files in the Current Directory
Problem
You want to see only hidden (dot) files in a directory to edit a file you
forget the name of or remove obsolete files. ls -a shows all
files, including normally hidden ones, but that is often too noisy, and
ls -a .* doesn’t do what you think it
will.
Solution
Use ls-d along with whatever other criteria you have.
ls -d .* ls -d .b* ls -d .[!.]*
Or construct your wildcard in such a way that . and .. don’t match.
$ grep -l 'PATH' ~/.[!.]* /home/jp/.bash_history /home/jp/.bash_profile
Discussion
Due to the way the shell handles file wildcards, the sequence .* does not behave as you might expect or desire. The
way filename expansion or
globbing works is that any string containing the
characters *, ?, or [ is treated as a pattern, and replaced by
an alphabetically sorted list of file names matching the pattern. *
matches any string, including the null string, while ? matches any
single character. Characters enclosed in [] specify a list or range of characters, any
of which will match. There are also various extended pattern-matching operators that we’re not going
to cover here (see “Pattern-Matching Characters” and “extglob Extended
Pattern-Matching Operators” in Appendix A). So
*.txt means any file ending in .txt, while
*txt means any file ending in
txt (no dot). f?o would match foo or fao but not fooo. So you’d think that .* would match any
file beginning with a dot.
The problem is that .* is expanded to include . and .., which ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access