Zsh: uncover hidden files in globs with (D)

Unlock what was hidden…

By default, Zsh globs skip hidden files and directories, meaning those with names starting with a dot. For example, in a project directory, * misses all the configuration files and directories:

$ ls -d *
README.rst  generate_pigeon.py

To include them, add the (D) glob qualifier, which stands for “dot files”:

$ ls -d *(D)
.env  .git  .github  .pre-commit-config.yaml  .venv  README.rst  generate_pigeon.py

(ls -d lists directories themselves, rather than their contents.)

(D) never matches the special . and .. entries, so you don’t need to worry about them popping up in results.

Recursive globs

(D) matters most for recursive globs with **/, because without it, Zsh also skips hidden directories. For example, to find all YAML files in a repository:

$ ls **/*.yml
zsh: no matches found: **/*.yml

That missed the GitHub configuration files within .github. Add (D) to find them:

$ ls **/*.yml(D)
.github/dependabot.yml  .github/workflows/main.yml

Sometimes skipping hidden directories is what you want, though. For example, **/*.py matches only generate_pigeon.py, usefully skipping the virtual environment in .venv, while **/*.py(D) includes all its files.

Always include hidden files with the glob_dots option

If you want globs to always include hidden files, enable the glob_dots option in your ~/.zshrc:

setopt glob_dots

Then you can use (^D) to exclude hidden files when needed, since ^ negates a qualifier:

$ ls -d *
.env  .git  .github  .pre-commit-config.yaml  .venv  README.rst  generate_pigeon.py
$ ls -d *(^D)
README.rst  generate_pigeon.py

List only hidden files

If you only want hidden files, you can start a pattern with a dot:

$ ls -d .*
.env  .git  .github  .pre-commit-config.yaml  .venv

Again, Zsh never matches . and .. here.

To find hidden files recursively, use **/.*, but you will need to add (D) so that Zsh also searches within hidden directories:

$ ls -d **/.*(D)
.env  .git  .github  .pre-commit-config.yaml  .venv  .venv/.gitignore  .venv/.lock

Fin

May what was hidden be found,

—Adam


Check out my new book Boost Your GitHub DX.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: